The subject of today’s post is the following rule from the Ruby Style Guide:
Array()instead of an explicit
Arraycheck, when dealing with a variable you want to treat as an Array, but you’re not certain it’s an array.
Countless times I’ve seen code like this:
It seems that
paths could be either an
Array object or an object
of some other class. The author of the code needed to make sure
paths would be an array so he creates a single element array in the
paths is not already an array.
While the above code works it’s not something an experienced Rubyist
would write. The most popular alternative is the use of the mighty splat
It case you’re puzzled by the preceding snippet consider the following example:
1 2 3 4 5 6 7
Hope that makes clear what’s going on.
While I’m extremely fond of this particular usage of
* I tend to
avoid it, since there is another equally powerful, but more readable
alternative to it –
Kernel#Array. Here’s it in action:
Array looks like the name of class, but it’s not. It’s a totally
normal method defined in the
Kernel module. There is a whole family
of conversion methods similar to
Array there –
String. They are all used
for often in practice and we’ll probably revisit them in a separate
post somewhere down the road.
Array method operates exactly like
* – it takes a single argument and
converts it to an
Array object if necessary:
1 2 3 4 5
You can also use
Array to convert to array other data
composite data structures (like hashes and sets), but that’s
irrelevant to today’s discussion.
That’s all I have for you today, mates. As usual I’m looking forward to hearing your thoughts here and on Twitter!