Reputation: 23532
I am coming from a Perl background and learning Ruby. I am bit confused about Ruby's hash syntax.
I am referring to the example given on http://www.ruby-doc.org/core-2.0/Hash.html
My Ruby throws a syntax error for the alternate syntax which is mentioned in the above link.
This code works fine:
#!/usr/bin/ruby
options = { :font_size => 10, :font_family => "Arial" }
puts options[:font_size]
Whereas, this code throws a syntax error:
#!/usr/bin/ruby
options = { font_size: 10, font_family: "Arial" }
puts options[:font_size]
The error is:
/home/workspace/ruby/so.rb:3: odd number list for Hash
options = { font_size: 10, font_family: "Arial" }
However, the link pasted above says that it should work.
Could you guide me what I am missing here? Or did I overlook something obvious?
BTW, here is the Ruby version that I am using:
# ruby --version
ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]
Upvotes: 0
Views: 335
Reputation: 59363
That syntax only works in Ruby 1.9 or later.
Note that the page you reference says "Ruby 2.0" in the title.
Upvotes: 4
Reputation: 168269
That syntax was introduced in Ruby 1.9. You are using a version earlier than that. You should not be using Ruby 1.8 any more. Look at this page: http://www.ruby-lang.org/en/news/2013/06/30/we-retire-1-8-7/.
Upvotes: 2