From: Florian Gross Date: 2004-09-18T19:24:44+09:00 Subject: Re: Accessing Hash elements in sorted order? ChrisO wrote: > When I saw the parens, I thought they might need to be there, but I > digressed from asking since it was simple enough in irb to experiment > and find out they were NOT needed. > > Still, it initially lead me as a fairly new player in Ruby, to consider > that the parens were perhaps some "mapping" mechanism since I had a > single item "pair" in that place before. Instead, when I realized the > parens were superfluous, I reasoned that Ruby was smart enough to "map" > the key/value on it's own it would seem. (Similar to #scan on Match > objects from the #scan regex? Kinda? If someone would care to > comment...?) It's all just Ruby's assignment semantics at work. I'm not sure if they're documented in the old Pickaxe at all, but if they are the documentation will be slightly out-dated. (Semantics were different in 1.6.8 and I think that between 1.8.0 and 1.8.1 they changed slightly for block parameters in respect to the difference between lambda { } and Proc.new) You can see the connections here: a = [1, 2] # a: [1, 2] a, = [1, 2] # a: 1 a = 1, 2 # a: [1, 2] -- equivalent to yield() sample giving warning a, b = [1, 2] # a: 1, b: 2 (a, b) = [1, 2] # a: 1, b: 2 -- same as a, b = *[1, 2] eval("yield [1, 2]") { |a| p a } # a: [1, 2] eval("yield [1, 2]") { |a,| p a } # a: 1 eval("yield [1, 2]") { |a,b| p [a,b] } # a: 1, b: 2 eval("yield [1, 2]") { |(a,b)| p [a,b] } # a: 1, b: 2 eval("yield(1, 2)") { |a| p a } # a: [1, 2] -- warning eval("yield(1, 2)") { |a,| p a } # a: 1 eval("yield(1, 2)") { |a,b| p [a,b] } # a: 1, b: 2 eval("yield(1, 2)") { |(a,b)| p [a,b] } # a: 1, b: 2 Regards, Florian Gross