What's the difference between these two options? They seem to do the same thing.
Asked
Active
Viewed 41 times
2 Answers
2
Both are the same the difference is Hash syntax for 1.9+ Ruby versions and 1.8- Ruby versions:
# Ruby 1.8 Syntax
{:this => 'syntax', 'is' => 'fun'}
# Ruby 1.9 syntax
{this: 'syntax', more: 'fun'}
Check this one as well: "What are the benefits of the new hash syntax in Ruby 1.9?"
As written in the referred post: It just looks nicer--it's syntactic sugar; it ends up being the same thing.

Community
- 1
- 1

mohamed-ibrahim
- 10,837
- 4
- 39
- 51
-
Please elaborate that your second example is in fact broken. – lorefnon Apr 29 '15 at 19:42
-
1What's broken? If the compiler is complaining, could that mean you're running on Ruby 1.8? – Katana314 Apr 29 '15 at 19:44
-
thanks @lorefnon fixed, Katana314 there was an extra quote – mohamed-ibrahim Apr 29 '15 at 19:45
-
Ok, the author changed the answer... http://stackoverflow.com/revisions/29953010/1 – lorefnon Apr 29 '15 at 19:46
1
There is no difference. The first is the older syntax for defining a key value pair for hashes. The second was introduced with Ruby 1.9 (I think).
If you're using a version of Ruby pre 1.9 you will have to use the first syntax, otherwise you should use the second syntax.

the Tin Man
- 158,662
- 42
- 215
- 303

margo
- 2,927
- 1
- 14
- 31
-
There is a difference. There are no "older" and "newer" syntaxes, the JavaScript-style syntax only applies to *some* symbols in Hash literals, if your keys are not a limited set of symbols then you need to use the hashrocket. – mu is too short Apr 29 '15 at 19:57