In Rails, we can see the following for example (They are separate here): :x=>"new_x" :y=>{:x=>X.new} From Ruby, I know that a hash has to be between { }, and the key is on the left of => and the value to the right. But, in the above examples we are seeing => everywhere! What is => ? Is it like = (assignment operator)? Thanks. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Abder-Rahman Ali wrote:> In Rails, we can see the following for example (They are separate here): > > :x=>"new_x" > > :y=>{:x=>X.new} > > From Ruby, I know that a hash has to be between { }, and the key is on > the left of => and the value to the right. > > But, in the above examples we are seeing => everywhere! What is => ? Is > it like = (assignment operator)? > > Thanks.This symbol (=>) is used to assign the value to the key. Hashes use this notation extensively. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> :x=>"new_x"Here x is the key and "new_x" is the value.> :y=>{:x=>X.new}Here y is the key and another hash, {:x => X.new}, is the value. And within this hash again, x is the key and X.new is the value. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Abder-Rahman Ali wrote:> In Rails, we can see the following for example (They are separate here): > > :x=>"new_x" > > :y=>{:x=>X.new} > > From Ruby, I know that a hash has to be between { }But you are wrong. The braces can be omitted when there is no ambiguity. Any time you see =>, a Hash is involved. That syntax has no other use.>, and the key is on > the left of => and the value to the right. > > But, in the above examples we are seeing => everywhere! What is => ? Is > it like = (assignment operator)? > > Thanks.-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Fri, Jul 16, 2010 at 09:02, Ram <yourstruly.vinay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > :x=>"new_x" > > Here x is the key and "new_x" is the value. > >> :y=>{:x=>X.new} > > Here y is the key and another hash, {:x => X.new}, is the value. And > within this hash again, x is the key and X.new is the value.I think what''s confusing him is that the outer hash isn''t within {}s -- he doesn''t realize that the {}s aren''t *always* necessary, that there are implicit hashes in some method calls. -Dave -- Specialization is for insects. | Professional: http://davearonson.com -Robert Anson Heinlein | Programming: http://codosaur.us -------------------------------+ Leadership: http://dare2xl.com Have Pun, Will Babble! -me | Et Cetera: http://davearonson.net -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Dave Aronson wrote:> On Fri, Jul 16, 2010 at 09:02, Ram <yourstruly.vinay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> �> :x=>"new_x" >> >> Here x is the key and "new_x" is the value. >> >>> :y=>{:x=>X.new} >> >> Here y is the key and another hash, {:x => X.new}, is the value. And >> within this hash again, x is the key and X.new is the value.More accurately the symbol :y is the key (splitting hairs)...> I think what''s confusing him is that the outer hash isn''t within {}s > -- he doesn''t realize that the {}s aren''t *always* necessary, that > there are implicit hashes in some method calls.Technically speaking Marnen''s reply ("The braces can be omitted when there is no ambiguity.") is more accurate. I also wanted to point out that there is a new "optional" syntax for hash key/value pairs introduced in Ruby 1.9: { :x: y } same as { :x => y } same as :x: y same as :x => y So what''s the business about ambiguity? my_obj.my_method(:x => y, :a => b) Was the intent of the above to call my_method with a single hash with two keys and two values, or to pass two single key hashes to my_method? Sometimes the {} are required to remove ambiguity: my_obj.my_method({ :x => y, :a => b })>> 1 hash argument containing 2 key/value pairsmy_obj.my_method({ :x => y }, { :a => b })>> 2 hash arguments each with a single key/value pairmy_obj.my_method([ { :x => y }, { :a => b } ])>> 1 array argument containing two hash elements, each having a single key/value pairmy_obj.my_method([ { :x => y, :a => b } ])>> 1 array argument containing one hash with two key/value pairs-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Walker wrote:> I also wanted to point out that there is a new "optional" syntax for > hash key/value pairs introduced in Ruby 1.9: > > { :x: y } same as { :x => y } same as :x: y same as :x => y[CORRECTION] Sorry, the above syntax is not quite right. New 1.9 syntax should be: y = 10 { x: y } => {:x=>10} Sorry for any confusion. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks all for your replies. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.