Have rvm.el set your emacs rvm environment via .rvmrc
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
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)
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:
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)
Archive
| May 2013 | ||||||
| S | M | T | W | T | F | S |
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 | |
March 2012
February 2012
November 2011
October 2011
April 2011
March 2011
January 2011
November 2010
October 2010
September 2010
August 2010
July 2010
June 2010
May 2010
April 2010
March 2010
February 2010
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 Categories
software (40)..cocoa (21)
..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
Request notifications






