Forum: Ruby difference between *options and options

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2009-01-07 06:05
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
6e366eb5a71be2bad7f383d42aeb4788?d=identicon&s=25 Justin Collins (Guest)
on 2009-01-07 06:58
(Received via mailing list)
Sijo Kg wrote:
>
> 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/mee...

-Justin
This topic is locked and can not be replied to.