Hi all,
I've noticed that for some methods in some gems, it's necesarry to pass
arguments like this:
method( :arg1 => value, :arg2 => value, :arg3,...)
I use the syntax the same way in the examples and it works, but I'd
like to know what Ruby's doing under there. What is the : and the =>
for anyway? Thanks for any info.
···
--
Posted via http://www.ruby-forum.com/.
It is the same as:
method( {:arg1 => value, :arg2 => value} )
{a => b} is a Hash in Ruby, :something is a Symbol. You can read about
those everywhere, I will not write yet another introduction.
Because the parser can easily see that it is a Hash, you can omit the {}.
On the other side, it will look like this:
def method(options = {})
arg1 = options[:arg1]
arg2 = options[:arg2]
end
It looks a bit like named arguments and has the same benefits: you can
reorder them as you like, you immediately know the meaning from the
argument list and you can omit optional arguments in a nice fashion.
Regards,
Florian
···
On Dec 13, 2009, at 11:26 PM, Omar Campos wrote:
Hi all,
I've noticed that for some methods in some gems, it's necesarry to pass
arguments like this:
method( :arg1 => value, :arg2 => value, :arg3,...)
I use the syntax the same way in the examples and it works, but I'd
like to know what Ruby's doing under there. What is the : and the =>
for anyway? Thanks for any info.