Apr 232012
 

Ruby 1.9 has a cool hash syntax which is quite similar to javascript, so the following hash

hash = {:symbol => "value"}

can be written as

hash = {symbol: "value"}

now, what if both key & values are symbols, in ruby 1.8 & before you would write

:pick => :any

but now, this could be written in short form as

pick: :any

And this both new & old syntax for hash are supported in ruby 1.9, you can use whatever syntax you are comfortable the most but it helps to know both forms of syntax specially when reading the code that other might have written. personally, I like the new one — fewer characters to type :P

 

PS: this new has syntax is applicable only if the key is a :symbol. In case when key is not a symbol. you have to still stick with old array syntax(=>)

Related posts:

  1. Understanding Differences Between Symbols & Strings in Ruby
  2. Ruby vs Java: Why do I like ruby more
  3. Contributing to Ruby on Rails – A Guide

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>