wbsurfver-/E1597aS9LQAvxtiuMwx3w@public.gmane.org
2013-Sep-27 19:05 UTC
notaion: Hash[*array_variable.flatten]
Can someone explain what the "*" operator inside the hash is doing here or what that notation is referred to in this context ? thanks irb(main):006:0* mail_config => {"server_setings"=>{"address"=>"smpt-int.ex1.com", "port"=>25}} irb(main):019:0> Hash[*mail_config[''server_setings''].map { |k, v| [k.to_sym, v.t o_s] }.flatten] => {:address=>"smpt-int.ex1.com", :port=>"25"} -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0ae0262b-4836-49ee-a5d8-86a3490cbbe1%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Try ... def test(arg1, arg2=nil, arg3=nil, arg4=nil) p arg1 p arg2 p arg3 p arg4 end # Without the splat test(mail_config[''server_setings''].map { |k, v| [k.to_sym, v.to_s] }.flatten) # [:address, "smpt-int.ex1.com", :port, "25"] # nil # nil # nil # With the splat test(*mail_config[''server_setings''].map { |k, v| [k.to_sym, v.to_s] }.flatten) # :address # "smpt-int.ex1.com" # :port # "25" So, the splat operator explodes an array into the arguments of the method. First case without the splat you can see that the whole Array goes into the arg1. Second, with the splat, each arg gets one of the Array elements. At your context the splat makes the call to Hash[] compatible with the form Hash[key1, value1, key2, value2, key3, value3, ...] One thing to note is: Hash[*mail_config[''server_setings''].map { |k, v| [k.to_sym, v.to_s] }.flatten] Hash[mail_config[''server_setings''].map { |k, v| [k.to_sym, v.to_s] }] # Without the splat and without flatten are equivalents (for the given example). # And you can do it differently in a way that I think is more intuitive (for my taste). mail_config[''server_setings''].each_with_object({}) { |(k, v), hsh| hsh[k.to_sym] = v.to_s } What do you think? Hope it helped you, Abinoam Jr. On Fri, Sep 27, 2013 at 4:05 PM, wbsurfver-/E1597aS9LQAvxtiuMwx3w@public.gmane.org <wbsurfver-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > Can someone explain what the "*" operator inside the hash is doing here or > what that notation is referred to in this context ? thanks > > > > irb(main):006:0* mail_config > > => {"server_setings"=>{"address"=>"smpt-int.ex1.com", "port"=>25}} > > > > irb(main):019:0> Hash[*mail_config[''server_setings''].map { |k, v| > [k.to_sym, v.t > > o_s] }.flatten] > > => {:address=>"smpt-int.ex1.com", :port=>"25"} > > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msgid/rubyonrails-talk/0ae0262b-4836-49ee-a5d8-86a3490cbbe1%40googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAH_NQoMmLaMg0gejMuV-8_2iG1Wcgmi_Q_XTC_E6AHNRZeZSAA%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.