This is part 2 of a 5-part series on Ruby tips and tricks gleaned from our team’s pull requests over the last two years. Part 1 is also available and covers blocks and ranges.
You’ve probably come across Ruby’s (de|re)structuring “splat” operator before, e.g.
This can be used in assignments too, in this next example it’s used both to
collect together the central elements into
body and to ‘break apart’ the
Arrays will be automatically destructured on the right hand side of an assignment, this works particularly nicely when you have a method returning an array.
I’m also partial to this trick for grabbing just the first element of an array.
However, the same result can be achieved more clearly with
You can take advantage of this for Hashes using the
#values_at method, which
returns an array.
This implicit destructuring also happens in block arguments.
Even cooler is you can force further destructuring with parentheses.
and this works in method parameters!
Ruby has two methods you can implement for your own classes so that you can
take advantage of this. The first to allow an object to be explicitly
The second, for implicit destructuring, is
#to_ary. You’ll want to be more
selective in implementing this one, as it can lead to your objects suddenly
behaving like arrays in places you weren’t expecting.
There’s one final use for the splat (
*); when overriding methods in a subclass
and calling super. If it turns out you don’t need the arguments to the method
for whatever additional behaviour you’re adding, you can accept all arguments
with a bare splat, then a bare
super passes all arguments to super.
We’ve just seen there are methods for implicit and explicit conversion of an object to an array. These can also be thought of as non-strict and strict conversion methods, and there are a few for other types.
The non-strict methods you probably know well,
#to_s, as of
#to_h, and a handful of others. These are available on a wide range
of classes, including
These non-strict conversions can sometimes trip you up, allowing invalid data to pass further down a system than you’d want, returning incorrect results, or producing unexpected errors.
Ruby provides some more strict conversion methods for a few types,
#to_str. These are only implemented on classes where the strict
conversion makes sense, e.g.
#to_int is only available on numeric classes.
This makes our example a little better, we’re getting an error and it’s coming
from the right place, but the error doesn’t really convey our full intent. Plus
the original was probably written that way to allow strings to be used, and as
String doesn’t implement
#to_int we’ve broken that.
But Ruby has another set of conversion methods, and these are a little more
intelligent. The most useful of these are
Integer() (along with
the other numeric conversion methods like
are less useful as they just delegate to
Now our example raises an error that really shows our intentions, plus we can
use strings again. This is where the
Integer() method really shines
Array() is also really useful, as when it can’t convert its argument to an
array it will put the argument in an array.
Occasionally you want to write a method that takes a single item, or an array
of items, and
Array() lets you do this without messing about checking types.
As we saw earlier with
#to_ary some of these conversion methods are used
internally to Ruby, these are the strict conversion methods, but can also be
thought of as the implicit conversion methods. They can be used implicitly
because they are strict.
These are used all over the place within Ruby, but as an example
used to convert the argument to
Array# to an int, and
#to_str is used by
raise when its argument isn’t an
One final conversion method to mention is
#to_proc. This is used by the unary
& operator in method arguments.
& converts a
Proc object into a block
argument in a method call.
But first unary
& will call
#to_proc on its operand, this allows it to be
used with objects that aren’t
Procs, but can be converted to one. A very
common use of this that you’ll find in modern Ruby code is using this with a
This has been built in to Ruby since version 1.9, but before that you could implement it yourself with some code like this:
Here’s an example of using
& to simplify initialising Points
from an array.
Head on to Part 3.