0

Possible Duplicate:
Is there any difference between :key => "value" and key: "value" assignments?
key in ruby new hash

I am reading some rails code and I see a method can be called

foo(:var1 => 'hello', :var2 => 'world')

or

foo(var1: 'hello', var2: 'world')

Both seems to work exactly the same. Is there any difference? Which is the better practice?

Community
  • 1
  • 1
Kirk
  • 517
  • 3
  • 14

2 Answers2

3

They mean the same thing. The latter is new Ruby 1.9 syntax inspired by JavaScript. Use the former if you need to stay compatible with Ruby 1.8. Otherwise it's a matter of taste.

mpartel
  • 4,474
  • 1
  • 24
  • 31
0

In a method call, both work exactly the same, but when you make a hash, they will have diferences

# in a method call
foo( :param => 'p' )
# mean the same thing
foo( param: 'p' )

# but in a Hash construction, they will have diferences

# here the key will always be a Symbol
hash = { symb: value }
# but here the key can be anything
hash = { 1 => "1" }
hash = { "1" => 1 }