Ruby and libxml: Collect your XML::Node::Set

Use collect and map in Ruby says Lucas Carlson. Thanks for pointing out that code arthritis, Lucas.

So, I was working with the libxml-ruby library (fast XML parsing) and found some loops in my code which could be converted. But, XML::Node::Set did not support collect. I mumbled something inaudible, grabbed the Pickaxe book, found the section on Enumerable, read for a bit, then just tried to mix it in:


# Mixes in Enumerable, to enable "collect" and friends
module XML
  class XML::Node::Set
    include Enumerable
  end
end

I put this at the top of my own library file, and suddenly I could do stuff like:


# photo is a XML::Node::set
photo.find('tags/tag').collect { |tag| tag.child.to_s }
# => ["funky", "rubylicious", "functional"]

This does an XPath expression, and walks through the result, collecting results, and then returns the whole chunk as an Array of tags as strings.

Thanks, Lucas, thanks libxml-ruby, thanks Matz.

*Update*: This mailing list post says that the above is going into 0.4, which seems to be coming along at a brisk pace. Some fantastic news in the shape of an upcoming patch that adds XmlTextReader API support. Good times.

Installing libxml-ruby for libxml2 with Ruby, on OS X

Just had an exciting package management adventure with Ruby and XML on OS X. I wanted Libxml2 wrapped in a Ruby gem. So, I ran

sudo gem install libxml-ruby

…and watched the software fail to build. Darn OS X not “just being Linux”. After complaining to Christoffer, I was kindly pointed to a MacPort:

sudo port install rb-libxml2

OK, that worked. But I don’t want to use DarwinPorts/MacPorts for my Rubygems, the gems change far too often for that. What I wound up doing was reinstall my gem (sudo gem install libxml-ruby), and then copy the libxml.bundle to its folder:


# cp /usr/local/lib/ruby/gems/1.8/gems/xml/libxml.bundle \
/usr/local/lib/ruby/gems/1.8/gems/libxml-ruby-0.3.8/ext/xml/

Also, go into the file

/usr/local/lib/ruby/gems/1.8/gems/libxml-ruby-0.3.8/ext/xml/libxml.rb

and change the required file’s name from xml/libxml_so to xml/libxml.bundle.

This was enough to make the thing work!

Moral: The right way to fix this would be to enhance the Makefile of the libxml-ruby gem to teach it about the OS X environment, but: not me, not today. This little tip is what I can do today. Ciao.

[tags]ruby,libxml2[/tags]

irb with wirble

Using irb with your Ruby? Yeah, I thought so.

If you want syntax coloring, ri inside irb and a shorter prompt, get Wirble:


sudo gem install wirble

And add an .irbrc file to your home directory, following Wirble’s advice on what to put there.

Oh, and when you have one of those, you could add Ben Bleything’s command history to your irb, while you’re at it. (Minor update: Ben’s called Ben, not Adam.)

Makes for a more communicative irb experience.

[tags]wirble,ruby[/tags]

Go snippet away, Rubyboy

Just started my morning with fixing a personal annoyance with the current Rails distribution I am using with Ruby 1.8.5: warning messages about deprecated syntax. All I did was heed the advice in the warnings and edit all the when and else control structures that were using colons to using semi-colons. After that, my Rails commands run silently.

High on simple fixes, I went to RubyForge, and beheld the fancy-looking web site, and went to look at Code Snippets. Whoa, lots of goodies there.
[tags]rubyonrails,ruby[/tags]