The adventure continues: New job, with Ruby




Kumputunturi

Originally uploaded by sushipumpum.

And Javascript. And all the rest a web developer does.

Now I commute to Malmoe, Sweden, to do this. I love it. A great team, here at Polar Rose.

I’ll post more on my progress and that when I get around to it. This is just the post which says the future looks fine, and I am beginning something very interesting.

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

Update 2012: This material was written in 2006. Now, many years later, it still poisons the search engines with its presence. If you want to use Ruby and a package manager on OS X, you should use RVM and Homebrew. I will leave this page here as a warning to you, dear reader. Do not take detailed old technical Ruby recipes as advice.

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.

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]

My Dashboard widget stopped working

A while ago me and my wife created a simple Dashboard widget that displayed our current amount of bonus accrued at the foodstore “club” we’re in. Since the website was just using a form which accepted member ID and PIN code, and the only thing you could do with the club website was enter see that amount, I figured I could afford a little scripting. A short script (and a lot of fiddling with the HTML, CSS, and Javascript for the widget) later, we had single-keystroke access to the number.

And then it happens. The widget displays “undefined”, instead of “24.00”. A bit of looking around later, it turns out the shopping center club has turned its simple form into a bigger “portal”. Now, the page for the login sits in a popup window, that shows this Flash movie. Darn. I’m not disassembling that SWF file, nor am I starting up Ethereal to monitor what it does. This time this throwaway script gets thrown away. So there.

[tags]dashboard,widget[/tags]

BarCamp here, in Copenhagen

On **17 november** at 17:00 [BarCamp Copenhagen](http://barcamp.org/BarCampCopenhagen) begins. What’s that? Let me quote [barcamp.org](http://barcamp.org/):

> BarCamp is an “ad-hoc” gathering born from the desire of people to share and learn in an open environment.

The activity is very simple:

> every person participating has to fill in a slot of 3 minutes for a presentation, idea, or anything related (or not related ?) to the people participating.

Interesting! You **get to present, for 180 seconds**!

![BarCamp Copenhagen](http://oschlag.dk/weblog/wp-content/uploads/2006/08/barcampcopenhagen.gif “Barcamp Copenhagen”)

**[Sign up at the wiki](http://barcamp.org/BarCampCopenhagen)**! That’s how to sign up for this event.

[Oschlag](http://oschlag.dk/) tells [more about it](http://oschlag.dk/weblog/2006/08/15/barcamp-copenhagen/), in Danish.

[tags]barcampcopenhagen[/tags]