MasterChef: Snapper en pappillotte with chunky chipsFor all my overseas readers (as if there is anyone in Australia *not* watching MasterChef), here is an awesome simple way to make Snapper en pappillotte with amazing chunky chips. 06:12 PM, 30 May 2009 by Mark Aufflick Permalink | Comments (1) Cookie Monster interviewedNice to see someone other than Elmo getting some media attention :) 05:24 PM, 19 May 2009 by Mark Aufflick Permalink | Comments (0) ruby 1.9 issue on Mac (MacPorts) : [BUG] unknown type 0x22 (oxc given)
From reading internet threads about the error, [BUG] unknown type 0x22 (oxc given), it seems to occur when C code compiled against ruby 1.8 is loaded into ruby 1.9.
At least using the MacPorts ruby 1.9 install there is a very odd issue related to this. You get a crash, under some circumstances, when a gem loads into ruby 1.9 and the MacOS linker decides it's a good idea to link against the standard OS installed version 1.8 libruby. What's especially odd is that the stack trace doesn't show it, but i did track it down by running lsof in a while /bin/true loop as I caused the crash: ... Now to find a way around it! Update: I may have found the culprit. The ruby core .dylib files (the Mac equivalent of .so files) are correctly linked to the ruby1.9 library, but the thin library (installed via gem1.9) is not: bash-3.2$ otool -L /opt/local/lib/ruby1.9/gems/1.9.1/gems/thin-1.2.1/lib/thin_parser.bundle Now I can manually tweak this with install_name_tool, but the gem build process must be broken somehow... Update 2: That wasn't the problem. I fixed the linking path in the thin_parser.bundle: sudo install_name_tool -change /usr/local/lib/libruby.dylib /opt/local/lib/libruby1.9.dylib /opt/local/lib/ruby1.9/gems/1.9.1/gems/thin-1.2.1/lib/thin_parser.bundle but it still crashes... Update 3: It's something to do with the MacPorts build. I did a compile from source of runby 1.9.1 into a new prefix and ruby/rack/thin work fine. Sweet! 05:19 PM, 10 May 2009 by Mark Aufflick Permalink | Comments (0) Functional map_each method for Ruby HashI prefer to use functional style programming wherever possible. In many popular dynamic languages such as Perl, Python or Ruby there is quite good support for functional programming with arrays such as Perl and Ruby's map and Python's even better list comprehensions. With hashes (or dictionaries or whatever you call them) you're usually stuck to sequential programming. Since in Ruby hashes are objects we can inject a nice map_each method into the Hash class which will process each key value pair and the resultant list will have one result for each pair. class Hash def map_each ret = [] self.each do |key, val| ret.push yield(key, val) end ret end end 02:53 PM, 09 May 2009 by Mark Aufflick Permalink | Comments (0) Opportunistically streaming html output with Ruby lambdasIn my last post (Ruby, HTML, s-expressions, lambdas?) I said I would show how we can modify the html generation to allow streaming. Of course I was making it out to be a lot more complicated in my head than it really is - all the hard work is done already by the lambdas enforcing correct execution order. All we need to do is add a few judicious STDOUT.flush calls before anything that might take some time (ie. evaluating the tag content which could include a database lookup etc.) I've done that in the new htm.rb below, but first, here's some demo code with some delays: engines = [["http://google.com/", "Google"], ["http://yahoo.com/", "Yahoo!"], ["http://webcrawler.com/", "Showing my age"]] Htm.htm(:ul, engines.map \ { |e| Htm.htm(:li, [:a, :href, e[0], [:b, lambda { sleep 3; Htm.str(e[1])}]])}).call Notice also how we can use a lambda anywhere we can put content. Instead of a sleep, perhaps it's an external ImageMagick command. Or perhaps instead of the map iterator our loop is iterating over results being streamed from a slow database query. Our output will look the same, but I've indicated with !!! where the output pauses for 3 seconds:
<ul><li><a href="http://google.com/"><b> !!! Google</b></a></li><li><a href="http://yahoo.com/"><b> !!! Yahoo!</b></a></li><li><a href="http://webcrawler.com/"><b> !!! Showing my age</b></a></li></ul>
You can see that the http stream will receive all the output it possibly can while waiting for the expensive operation, and the tags are still automagically closed. Job done! Here's the up to date htm.rb: require 'cgi' module Htm def Htm.str(content) lambda { print CGI.escapeHTML(content.to_s) } end def Htm._build_tag(tag, args) # open the tag print '<' + tag.to_s tok = args.shift # find any tag attribute key/value pairs while tok.kind_of? Symbol print ' ' + tok.to_s + '="' + CGI.escapeHTML(args.shift.to_s) + '"' tok = args.shift end if tok.nil? # self-close tag if no content print ' />' else # finish open tag print '>' # output tag content Htm._eval_content( tok.kind_of?(Symbol) ? Htm.htm(tok, args) : tok ) # close tag print '</' + tag.to_s + '>' end end def Htm._eval_content(content) while content.kind_of? Proc STDOUT.flush content = content.call end if content.kind_of? Array Htm._eval_content(Htm.htm( *content )) elsif ! content.nil? print content end end def Htm.htm( *args ) lambda do tok = args.shift while ! tok.nil? if tok.kind_of? Symbol Htm._build_tag(tok, args) else Htm._eval_content(tok) end tok = args.shift end end end end 10:02 AM, 09 May 2009 by Mark Aufflick Permalink | Comments (0) Ruby, HTML, s-expressions, lambdas? Oh my!I've been noodling around with various lisps lately, and one of the sources of lisp's elegance is the lack of separation between data and code. A practical example of that is the cl-who library which creates html from an s-expression representation, ensuring all tags are correctly closed. For example: (with-html-output (*http-stream*) (loop for (link . title) in '(("http://zappa.com/" . "Frank Zappa") ("http://marcusmiller.com/" . "Marcus Miller") ("http://www.milesdavis.com/" . "Miles Davis")) do (htm (:a :href link (:b (str title))) :br))) There are a number of ways to replicate this style in other languages. You can do it by building up a big string, but I want to print to a stream like the example above. Using lambdas we can make a pretty good approximation in Ruby. Here's some Ruby code using a simple module I whipped up (see below) to achieve the same output as the cl-who lisp code above: engines = [["http://google.com/", "Google"], ["http://yahoo.com/", "Yahoo!"], ["http://webcrawler.com/", "Showing my age"]] Htm.htm(engines.map { |e| Htm.htm(:a, :href, e[0], [:b, Htm.str(e[1])], :br, nil)}).call Lets say we realise that the br tags are nasty and want to put the list of links into a list: Htm.htm(:ul, engines.map \ { |e| Htm.htm(:li, [:a, :href, e[0], [:b, Htm.str(e[1])]])}).call It's not quite as elegant as the lisp equivalent because we are mixing lambdas and arrays - both as nesting constructs. Here is the final output (indented for clarity - the Htm module below does no indenting): <ul> <li><a href="http://google.com/"><b>Google</b></a></li> <li><a href="http://yahoo.com/"><b>Yahoo!</b></a></li> <li><a href="http://webcrawler.com/"><b>Showing my age</b></a></li> </ul> Now even though we are controlling the order of execution such that the various parts will be printed to the stream in the right order, we still can't print until we have closed our outermost tag (usually html). To be able to stream output while also auto-closing the tags we're going to need to use continuations - I'll cook up an example of that for a later blog. There's also other neat ways we can have self-closing tags that will allow streaming - I'll post about that later also. Here is the Htm module in full: require 'cgi' module Htm def Htm.str(content) lambda { print CGI.escapeHTML(content.to_s) } end def Htm._build_tag(tag, args) # open the tag print '<' + tag.to_s tok = args.shift # find any tag attribute key/value pairs while tok.kind_of? Symbol print ' ' + tok.to_s + '="' + CGI.escapeHTML(args.shift.to_s) + '"' tok = args.shift end if tok.nil? # self-close tag if no content print ' />' else # finish open tag print '>' # output tag content Htm._eval_content( tok.kind_of?(Symbol) ? Htm.htm(tok, args) : tok ) # close tag print '</' + tag.to_s + '>' end end def Htm._eval_content(content) while content.kind_of? Proc content = content.call end if content.kind_of? Array Htm._eval_content(Htm.htm( *content )) elsif ! content.nil? print content end end def Htm.htm( *args ) lambda do tok = args.shift while ! tok.nil? if tok.kind_of? Symbol Htm._build_tag(tok, args) else Htm._eval_content(tok) end tok = args.shift end end end end 09:13 PM, 06 May 2009 by Mark Aufflick Permalink | Comments (0) Adding Apple Remote Support to your Cocoa AppHere is the presentation I gave at last month's CocoaHeads: Or you can download the pdf from the CocoaHeads AU Google group: apple_remote.pdf. 10:24 AM, 05 May 2009 by Mark Aufflick Permalink | Comments (0) CocoaHeads Sydney - May 7th 2009 CocoaHeads Sydney is steaming along with our fourth monthly meeting on the trot this Thursday.Last month at CocoaHeads Sydney I gave a lightning talk showing how to add Apple IR support to your Cocoa app. I have uploaded the slides to the files section of the google group. In the presentation I also showed how I used the LaTeX beamer class to make pdf slides, and I used the open source SplitShow application to give the presentation (using my Apple IR support of course!) You can get SplitShow from the google code project but you'll have to build from the trunk to get the IR support which has not yet been included in a release. We also had Ben Hill show us the impressive prototype of an OpenGL iPhone game he put together using cocos2d. This month our very own Jude Sutton will be giving us a presentation on the Authorisation Services and related APIs. This month's details:
When: Thursday May 7th, 6:30pm All details are published monthly on the CocoaHeads AU google group. Hope to see you there! 10:12 PM, 03 May 2009 by Mark Aufflick Permalink | Comments (0) |
Archive
January 2010 October 2009 September 2009 August 2009 July 2009 June 2009 May 2009 April 2009 February 2009 January 2009 December 2008 November 2008 October 2008 September 2008 August 2008 July 2008 June 2008 May 2008 March 2008 February 2008 January 2008 December 2007 November 2007 October 2007 September 2007 August 2007 July 2007 June 2007 May 2007 April 2007 March 2007 February 2007 January 2007 December 2006 November 2006 October 2006 September 2006 August 2006 July 2006 June 2006 May 2006 April 2006 March 2006 February 2006 January 2006 December 2005 November 2005 October 2005 September 2005 August 2005 July 2005 June 2005 May 2005 April 2005 March 2005 February 2005 January 2005 December 2004 November 2004 October 2004 September 2004 August 2004 July 2004 June 2004 May 2004 April 2004 March 2004 February 2004 January 2004 December 2003 November 2003 October 2003 September 2003 August 2003 Blog Categoriessoftware (24)..cocoa (12) ..heads up 'tunes (5) ..ruby (4) ..lisp (1) ..perl (2) ..openacs (1) mac (18) embedded (2) ..microprocessor (2) ..avr (1) electronics (3) design (1) Notifications Request notifications
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||







CocoaHeads Sydney is steaming along with our fourth monthly meeting on the trot this Thursday.

Request notifications