-2

What's the difference between :foo => :bar and foo: :bar?

Are there circumstances where I should be using one over the other?

What are the advantages/disadvantages?

EDIT: In defense of this not being a dup.

The question/answer linked to by the mods does not:

  • Comment on the conventionality of using either style which I feel is a important factor in deciding to use one over the other.
  • Address that in my question the values are symbols.
  • Which versions of Ruby started using the new style
franksort
  • 3,093
  • 2
  • 19
  • 27
  • 3
    `foo: :bar` was introduced in Ruby version 1.9 as an acceptable alternative syntax to `:foo => :bar`. They mean the same thing: a hash value. Here's a commentary on it: http://logicalfriday.com/2011/06/20/i-dont-like-the-ruby-1-9-hash-syntax/ – lurker Feb 14 '14 at 14:55
  • Does that mean that `:foo => :bar` is more conventional and `foo: :bar` is just tolerated? (Sort of like using `return` vs not using `return`.) – franksort Feb 14 '14 at 14:58
  • @franksort - not at all. Ruby introduced a new syntax that many systems, including Rails, have now adapted. Both are totally acceptable and "equally ranked" syntax for hash elements. The only difference is that Ruby versions < 1.9 will flag `foo: :bar` as an error. Your example of explicit `return` versus implicit `return` would be similar: neither of these approach is considered really inferior, but a matter of personal choice in programming. – lurker Feb 14 '14 at 15:03
  • @mbratch Very interesting. Do you recall why it was added to the language? – franksort Feb 14 '14 at 15:20
  • lol no, I wasn't on the committee. :) I suspect because the old syntax is a bit cumbersome when passing several hash parameters to a method. – lurker Feb 14 '14 at 15:26
  • Re: "In defense of this not being a dup". The previously answered question does cover all those points in the answers given to it. No one answer needs to cover all the points; The answers given as a group do so. – the Tin Man Feb 14 '14 at 19:42

1 Answers1

3

Most of the time there's no difference, if you're running a version of Ruby (v.1.9+) that supports the foo: :bar notation.

The advantages/disadvantages are mostly programmer preferences, except for complex keys we could often use :"foo bar", but that isn't supported for the newer syntax:

ash = {:'foo foo' => 1}
=> {:"foo foo"=>1}
irb(main):002:0> hash = {'foo foo': 1}
SyntaxError: (irb):2: syntax error, unexpected ':', expecting =>

Otherwise, you can use whichever feels better to you. I'd recommend that you don't mix them up though, as that becomes a readability, then a maintenance, issue.

At work I prefer to see => because it's a more visible delimiter for key/value pairs, which then also allows us to run the code on more versions of Ruby, if that was necessary.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • Good comments. I think the newer syntax, on the flip side, makes the passing of several hash parameters to a method less cumbersome (and may be, in part, what drove the change). – lurker Feb 14 '14 at 15:31
  • Is it possible to add to this answer that, as @mbratch points out, the newer syntax is only valid with Ruby >= 1.9? – franksort Feb 14 '14 at 16:06