Star in Ruby
March 15, 2014
I have seen the Ruby’s *
operator used at several places in different
ways and often times I get confused the way it is used. Finally I decided to learn more about it and here are few interesting roles of *
operator that I found in Ruby.
It can be used to multiply, repeat, copy or as a splat operator.
Multiplication
This is the most obvious role of *
. It’s the multiplication operator. 2 * 2 = 4
Or an instance method of Fixnum class to perform multiplication. 2.*(2) = 4
Repetition
Several classes in Ruby defines *
operator as a Repetition method.
- Array class defines * for two different possible type of parameters.
For string type it acts as a join
method. Examples:
- String class defines * as a copy operator. It returns a new string containing n copies of the string. For example:
Note that String * str
is not defined.
Splat
Another less known use of *
is as a splat operator. Consider
following example where we want to convert options_array to
options_hash.
You will see that []
method in Hash class takes [key, value, …] as an argument and
creates a Hash for you. For example:
So how can we use []
method to get the required hash? You can see in
documentation that []
doesn’t
take array as a parameter or you can try running it. It will return you an empty hash with lot of warnings. So we can’t call Hash[options_array]
directly.
- We can use splat
*
operator to convert our array into arguments. If you call[]
with*options_array
as a parameter, you’ll get the required hash back.
So splat converts *options_array
to an argument list. Let’s see some
other interesting uses of splat operator.
Splat can be used to convert an array to argument list as shown above. You can call methods by dynamically generating arguments with minimal amount of code. For example, if you have a string with ‘,’ separated values of arguments.
You can call do_it
method with arg1
, arg2
and arg3
as arguments
using splat like this:
- You can also convert list of arguments back to array using splat. For example:
Note that you don’t need to use splat for the above code to work.
is equally valid. But it’s good to know that you can do it using splat also.
However, a case where splat comes in handy is to define multiple variables. Let’s see an example:
Above fact is mostly used in defining methods with optional arguments. This is one of the most common uses of the splat operator.
It is not necessary to have the splat argument at the end in the method! For example definition like this is equally valid:
However it’s invalid to have more than 1 splat arguments.
- You can also use splat operator to convert Hash into an array of arrays. For example:
- You can use splat operator to un-nest arrays. For example:
Splat operator can possibly be used in several creative ways. If you know some other way that splash could be used feel free to suggest it in comments.
References/Further Reading:
- //endofline.wordpress.com/2011/01/21/the-strange-ruby-splat/
- //www.jacopretorius.net/2012/01/splat-operator-in-ruby.html
- https://www.ruby-doc.org/