Ruby Quicktips Logo

Ruby Quicktips

Random Ruby and Rails tips.
This blog is dedicated to deliver short, interesting and practical tidbits of the Ruby language and Ruby on Rails framework. Read more...

Your submissions are more than welcome!
May 9 ’11

Hash: new create syntax

Additional to the syntax for creating hashes we all know:

hash = { :first => "ruby", :second => "rails" }
# => {:first=>"ruby", :second=>"rails"}

hash[:first] # => "ruby"

…Ruby > 1.9.1 adds support for another syntax:

hash = { first: "ruby", second: "rails" }
# => {:first=>"ruby", :second=>"rails"}

# you still have to access a value via a symbol
hash[:second] # => "rails"
hash[second]  # NameError

6 notes 0 comments

  1. kasei-san reblogged this from rubyquicktips
  2. toronya reblogged this from rubyquicktips
  3. devlim submitted this to rubyquicktips

Comments

You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.

blog comments powered by Disqus