Begin main content

Have rvm.el set your emacs rvm environment via .rvmrc

rvm has this great feature where you can put stuff into a .rvmrc file per project to ensure when you cd into any subdirectory of a given rails project you will have the right ruby/gem paths etc.

rvm.el enables that same environment to be set (globally) for an emacs process, with functions like rvm-activate-corresponding-ruby.

To get rvm.el to update globally when you cd into a dir in your emacs shell, just add the following to your .rvmrc after the rvm call to set the ruby/gemset:

emacsclient -n --eval '(rvm-activate-corresponding-ruby)'

(which of course assumes that your .emacs has (server-start) or similar).

07:26 AM, 05 Mar 2011 by Mark Aufflick Permalink | Comments (0)

Apple Push Notifications Talk—Screen/Audio recording now available

Thanks again to the guys at the Apple University Consortium for inviting me to speak at /dev/world 2010. The screen/audio recordings are now available. I have linked to them (and the rest of the /dev/world presentations) on my APNS talks page:

http://mark.aufflick.com/talks/apns

07:00 PM, 02 Mar 2011 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:

...
ruby1.9 41978 aufflick txt REG 14,2 3148976 67264 /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/libruby.1.dylib
...
ruby1.9 41978 aufflick txt REG 14,2 1906476 3915360 /opt/local/lib/libruby1.9.1.9.1.dylib
...

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
/opt/local/lib/ruby1.9/gems/1.9.1/gems/thin-1.2.1/lib/thin_parser.bundle:
/usr/local/lib/libruby.dylib (compatibility version 1.8.0, current version 1.8.6)
...

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!

03:19 AM, 10 May 2009 by Mark Aufflick Permalink | Comments (0)

Functional map_each method for Ruby Hash

I 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.

classHashdefmap_each
    ret = []
    self.each do |key, val|
      ret.push yield(key, val)
    end
    ret
  endend

12:53 AM, 09 May 2009 by Mark Aufflick Permalink | Comments (0)

Opportunistically streaming html output with Ruby lambdas

In 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><ahref="http://google.com/"><b> !!! Google</b></a></li><li><ahref="http://yahoo.com/"><b> !!! Yahoo!</b></a></li><li><ahref="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'moduleHtmdefHtm.str(content)
    lambda { print CGI.escapeHTML(content.to_s) }
  enddefHtm._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
    endif 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 + '>'endenddefHtm._eval_content(content)
    while content.kind_of? ProcSTDOUT.flush
      content = content.call
    endif content.kind_of? ArrayHtm._eval_content(Htm.htm( *content ))
    elsif ! content.nil?
      print content
    endenddefHtm.htm( *args )
    lambda do

      tok = args.shift

      while ! tok.nil?
        if tok.kind_of? SymbolHtm._build_tag(tok, args)
        elseHtm._eval_content(tok)
        end
        tok = args.shift
      endendendend

08:02 PM, 08 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><ahref="http://google.com/"><b>Google</b></a></li>
  <li><ahref="http://yahoo.com/"><b>Yahoo!</b></a></li>
  <li><ahref="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'moduleHtmdefHtm.str(content)
    lambda { print CGI.escapeHTML(content.to_s) }
  enddefHtm._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
    endif 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 + '>'endenddefHtm._eval_content(content)
    while content.kind_of? Proc
      content = content.call
    endif content.kind_of? ArrayHtm._eval_content(Htm.htm( *content ))
    elsif ! content.nil?
      print content
    endenddefHtm.htm( *args )
    lambda do

      tok = args.shift

      while ! tok.nil?
        if tok.kind_of? SymbolHtm._build_tag(tok, args)
        elseHtm._eval_content(tok)
        end
        tok = args.shift
      endendendend

07:13 AM, 06 May 2009 by Mark Aufflick Permalink | Comments (0)

XML

Blog Categories

software (41)
..cocoa (23)
  ..heads up 'tunes (5)
..ruby (6)
..lisp (4)
..perl (4)
..openacs (1)
mac (21)
embedded (2)
..microprocessor (2)
  ..avr (1)
electronics (3)
design (1)
photography (26)
..black and white (6)
..A day in Sydney (18)
..The Daily Shoot (6)
food (2)
Book Review (2)

Notifications

Icon of envelope Request notifications

Syndication Feed

XML

Recent Comments

  1. Mark Aufflick: Re: the go/Inbox go/Sent buttons
  2. Unregistered Visitor: How do make a button to jump to folder
  3. Unregistered Visitor: Note I've updated the gist
  4. Unregistered Visitor: umbrello is now an available port on macPorts
  5. Unregistered Visitor: Updated version on Github
  6. Unregistered Visitor: Modification request.
  7. Unregistered Visitor: Accents and labels with spaces
  8. Unregistered Visitor: Mel Kaye - additional info
  9. Unregistered Visitor: mmh
  10. Mark Aufflick: Thank you