Possible Duplicate:
Is there any difference between the:key => “value”andkey: “value”hash notations?
What’s the difference between this:
method: :delete
and this?
:method => :delete
I’m guessing it has to do with different versions of Rails but not sure. I have only worked in Rails 3.x.
They are completely equivalent, except the first can only be used since ruby 1.9 (and higher, of course).
In ruby 1.8 the hash syntax used the
=>, also known as thehash rocket. You could put anything in front, and anything behind, but the thing in front is your key, behind the value. If you have a symbol as key, and a symbol as value, you would write:But you could also write
Now, for ruby 1.9.x, a new shorter syntax was introduced. Since most people use symbols as keys, you can now write:
Which is just a shorter/cleaner version. Also note that it is possible to mix both styles, which in some cases is needed.
E.g. in ruby 1.8 you would write:
This would translate to the following in ruby 1.9
Note that you can still keep using the “old” hash syntax as well. It is a matter of preference. Personally for hashes with only symbols as keys, I use the clean/short version. I generally try not to mix hash-style in a single hash 🙂