codesnips’s posterous

« Back to blog

[Ruby] Splat operator

expand array items into individual arguments.


>> ar
=> ["a", "b"]
>> ar.class
=> Array

>> def ar_in(*args)
>>    puts args, args.length
>> end

without splat
?> ar_in(ar)
a
b
1

this is what we want
>> ar_in('a', 'b')
a
b
2

which is possible by using the splat operator before the array

>> ar_in(*ar)
a
b
2

Comments (0)

Leave a comment...

 
To leave a comment on this posterous, please login by clicking one of the following.
Posterous-login     Connect     twitter