ruby method handedness

Mon, Dec 15, 2008

Once I’d binned Launchy and just used an OS X specific way of launching the browser for my new mini project, FlikBak, a Flickr backup utility, I got as far as listing all my photosets and getting down to image URLs. Then I ran the same code on the laptop. Big mistake! On the big mac I’m using 1.8.7-p72 and this works fine:

photosets = REXML::XPath.each(doc, “//rsp/photosets/photoset”)
photosets.each do |photoset|
  title = REXML::XPath.first(photoset, “title”)
  description = REXML::XPath.first(photoset, “description”)
   …
end
but on the laptop I’m running 1.8.6 and the same code gives the error:
ruby/1.8/rexml/xpath.rb:53:in `each’: no block given (LocalJumpError)
So I looked a but more closely at the code. Seems I have the wrong mental model of how XPath.each works. I’ve been using XPath.first a lot and the result comes out the left side of the expression. XPath.each comes out the other end! This works:
REXML::XPath.each(doc, “//rsp/photosets/photoset”) do |photoset|
  title = REXML::XPath.first(photoset, “title”)
  description = REXML::XPath.first(photoset, “description”)
  …
end
At least it’s interesting and I’m getting somewhere with FlikBak. Might even add a UI to it.

comments powered by Disqus