difference between *options and options

S

Sijo Kg

Hi
I saw below code

options = { :a => 1, :b => 2, :c => 3 }
rejects = Hash[*options.select { |k, v| k == :b && options.delete(k)
}.flatten]

In the above if I use options insted of *options what is actually
happening..I would like to know their difference What actually means
*options

Thanks in advance
Sijo
 
J

Justin Collins

Sijo said:
Hi
I saw below code

options = { :a => 1, :b => 2, :c => 3 }
rejects = Hash[*options.select { |k, v| k == :b && options.delete(k)
}.flatten]

In the above if I use options insted of *options what is actually
happening..I would like to know their difference What actually means
*options

Thanks in advance
Sijo

This code might be a little deceptive, but if you look closely
"options.select { |k, v| k == :b && options.delete(k)}.flatten" is just
creating an array out of selected keys and values. After that, the splat
operator (*) is being used to turn the array into parameters for the
Hash.[] function...which turns it back into a hash. Tada!

I note that in Ruby 1.9.1, Hash#select returns a hash, making this
weirdness unnecessary.

More about splat, including this particular use:
http://advent2008.hackruby.com/past/2008/12/12/meet_the_splat/

-Justin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top