Robert Klemme
2/12/2009 5:26:00 PM
On 12.02.2009 17:51, Jonatas Paganini wrote:
> Hi, I'm looking for cucumber/generators and didn't understand why use
> '*' before string on initialize method.
> I put it on irb and the result is the same.
>
> class NamedArg
> attr_reader :name
>
> def initialize(s)
> @name, @type = *s.split(':')
> end
> end
>
> Why and where use * before string?
It's not before a String but before an Array. You can see for yourself
by firing up IRB and experimenting a bit with this. The short story is
that it's called "splash operator" IIRC and it will distribute the Array
across all parameters or local variables. Other example uses are
def foo(a,b,c)
puts a,b,c
end
foo(1,2,3)
ar = [1,2,3]
foo(*ar)
foo(ar) # -> error because you do not provide enough arguments
HTH
Kind regards
robert