Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
difference between *options and options
Sijo Kg
1/7/2009 5:07:00 AM
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
--
Posted via
http://www.ruby-...
.
1 Answer
Justin Collins
1/7/2009 5:51:00 AM
0
Sijo Kg wrote:
> 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_...
-Justin
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
difference between *options and options
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password