Learning Ruby

Today’s project has been to get my head sufficiently around Ruby to be able to write Facter “facts”. We’re working on fitting Puppet to our needs, but the facts Facter knows out of the box aren’t sufficient.

The first mistake was trying to do this in the office. Too noisy. Fortunately my employer is sane enough about these things that I was able to go off and quietly work on this at home.

The second mistake was trying to use the Ruby in 20 Minutes tutorial. I can’t quite put my finger on what it was, but there was both enough stuff missing for me to not get the context I needed and also too much time taken up with fluff at the start.

Fortunately the Learning Ruby material is much better. It took more than 20 minutes, but left me with enough grounding in the language to be able to understand sample Facter recipes where they had previously looked like gibberish.

The key things I think you need to know when starting with Ruby are:

  1. Everything is an object. Including literals;
  2. Methods can have blocks of code passed to them and frequently do.

These two matter because, if you’re coming from something like Perl, code like:


things=['one', 'two', 'three']
things.each do | n | { puts n }

looks pretty weird, and so does:


m='wibble'.match('/bbl/')

(I’m not sure how idiomatic either of these are, but I’ve seen them in bits of example code and they did indeed seem very odd. Arse-backward, even.)

Anyway.

I feel I’m at a point now where I have some hope of being able to read existing facts and write my own that aren’t simple cargo-cults. I’ve already written some that let us tell what city the host is in based on IP address and will probably be writing more as I go along.

Popularity: 32% [?]

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Slashdot

Discussion Area - Leave a Comment