nice idioms!
This is the first post in a new category I’ve added to the blog: “Ruby Skills”. It’s a place for me to share Ruby tricks and tips I’ve picked up along the way. Sometimes, as with this post, I’ll post about the Ruby extensions in HoboSupport. Now that HoboSupport is available as a gem, you can easily use these tricks in any Ruby project.
First up, two new Enumerable methods that HoboSupport adds: * and where. Attentive readers might be thinking — hang on, Array already defined *. Don’t worry, it still works.
* is some syntactic sugar for map. The idea is that we use ‘dot’ to call a method on one object, and we use ‘dot star’ to call a method on a whole collection of objects, returning all the results in a new array.
Say users is an array of user objects, and we want all the names:
users.*.name
Nice eh? You can pass arguments too:
users.*.to_json(:only => [:first_name, :surname])
Note that you can’t do
users.*.name.upcase
That would try to upcase the array. You’d have to do:
users.*.name.*.upcase # Not very efficient though
Of course, as a good functional programmer, I wouldn’t dream of giving map some love while neglecting filter (better known in Ruby-land as find_all or select). So you can also do:
users.where.active? # same as users.find_all {|u| u.active? }
There’s also where_not
Given that the result is just an array, we can chain them. Want the names of all the inactive users?
users.where_not.active?.*.name
Very handy in the console.
nice idioms!
Will this create a conflict when Ruby 1.9 adds the * operator?
http://pragdave.blogs.pragprog.com/pragdave/2008/06/silly-ruby-19-t.html
Hi Brandon – no I think it should be OK. Actually the ’splat’ operator is already there:
>> x = [1,2,3]
=> [1, 2, 3]
>> [10, *x]
=> [10, 1, 2, 3]
It’s syntactically different from the multiply operator (note there’s no left-hand-side). Ruby 1.9 just adds a load of new tricks you can do with it.
Great stuff!