* used both for the operator ( 42 * 42 ) and for unpacking the arguments ( myfun *[42, 42] ).
When you do:
mx *mx 2/mx *mx
Ruby interprets this as unpacking the arguments, not the * operator (i.e., multiplication).
If you are not familiar with it, unpacking the arguments (sometimes also called "splat" or "splats") means that you can have a function like this:
def myfun arg1, arg2; end
And name it as follows:
myfun(*['Hello', 'World'])
arg1 set to Hello , and arg2 set to World .
I believe that the rules should determine what to use:
- Space before, but not after
* → Unpacking arguments - Function parenthesis start → Unpacking arguments
- Everything else → Multiplication (or rather, the
* operator, since Ruby performs operator overloading).
Good recommendations:
- Use the "optional" function in brackets when you plan to unpack the arguments;
- use spaces before and after
* when you intend to use the * (multiply) operator.
Ruby will really warn you when running ruby -v :
test.rb|11 warning| `*' interpreted as argument prefix test.rb|12 warning| `*' interpreted as argument prefix
source share