Is there a difference between writing
parameter: "String"
and the lengthier
:parameter => "String"
Is there a difference between writing
parameter: "String"
and the lengthier
:parameter => "String"
They both construct a hash key-value pair. For the most part they are interchangeable. The parameter: "String"
syntax was introduced in Ruby 1.9 and is modeled after JSON.
There are some differences/gotchas. The biggest one is that the newer syntax doesn't handle quoted keys. In a 1.8-style hash, you can do this:
:"multi word key" => some_value
In a 1.9-style hash, this is a syntax error:
"multi word key": some_value
No difference.
I prefer 2nd syntax as I think it's easier to see when doing brief overview.
In Ruby 1.9.x and 2.0.0
It's just a syntactic Sugar.
If you see in irb with Ruby 1.8.7
1.8.7 :004 > {:name => 'String'}
=> {:name=>"String"}
and With 1.9.3
1.9.3p392 :002 > {name: 'String'}
=> {:name=>"String"}
Both return the same format.