user419017
user419017

Reputation:

Is this valid ruby syntax?

hash = {:symbol, 'mental'}

Because I'm finding the above dotted around a project I'm working on, and I can't figure out whether this is due to an older version of Ruby or not.

Upvotes: 1

Views: 91

Answers (2)

Rahul Tapali
Rahul Tapali

Reputation: 10137

Valid in Ruby 1.8 when you have right key value pair in order. And you need to have even number of elements in hash creation:

hash = {:symbol, 'mental'} same as hash = {:symbol => 'mental'}

Some examples:

hash = {:symbol, 'mental', :name,'abc'}
 => {:symbol=>"mental", :name=>"abc"}

hash = {:symbol, 'mental', :name,'abc', :b}
=>  Syntax Error

Upvotes: 10

Digikata
Digikata

Reputation: 1959

Seems like a typo. I think that line should read:

hash = {:symbol => 'mental'}

This does seem to be valid for 1.8.7 ruby though...

Upvotes: 1

Related Questions