Friday, 29 June 2012

hash syntax change in ruby 1.9

Hi,

In ruby 1.9 hash syntax has been changed. Use the new syntax to over come the problem while upgrading some gems or plugins to future versions.

# Ruby 1.8 Syntax
{:name => 'uma mahesh', 'phone' => '9866439593'}
# Ruby 1.9 syntax
{name: 'uma mahesh', 'phone': '9866439593'}


hash keys should accessed with symbols as below

profile = {:name => 'uma mahesh', 'phone' => '9866439593'}

profile['name'] # => nil

profile[:name] => 'uma mahesh'

The new syntax mimics JSON which is important for Ruby, or at least Rails, developers as JavaScript is one of the languages we’ll switch to more frequently.


Its is similarity to JavaScript's object notation, and looks a bit like JSON



Thank You,
Uma Mahesh.

No comments:

Post a Comment