Question

I know it both symbol: "value" and symbol => "value" are valid syntax.

Currently, I have no standard which one to be used. For example in image_tag, sometimes I write class: "css_class", but sometimes in model has_many, I use :through => :line_items.

Often, I mixed these two even in one code sentence and complex myself.

I really would like to hear your suggestions on which is the best (proper) one to use in particular situations. (I know it both are proper, but more expressive, clean and beautiful code.) May be your preferences.

Was it helpful?

Solution

To help you decide which hash literal syntax to use, check out:

Snippet from the Ruby Style Guide on Collections:

Preferably use symbols instead of strings as hash keys.

# bad
hash = { 'one' => 1, 'two' => 2, 'three' => 3 }

# good
hash = { one: 1, two: 2, three: 3 }

Use the Ruby 1.9 hash syntax when your hash keys are symbols.

# bad
hash = { :one => 1, :two => 2, :three => 3 }

# good
hash = { one: 1, two: 2, three: 3 }

Don't mix the Ruby 1.9 hash syntax with hash rockets in the same hash literal.

When you've got keys that are not symbols stick to the hash rockets syntax.

# bad
{ a: 1, 'b' => 2 }

# good
{ :a => 1, 'b' => 2 }

OTHER TIPS

As you probably know, the colon (key: value) syntax was introduced in Ruby 1.9. If you're writing a library that you're going to distribute to people who might be running an earlier version of Ruby, use the arrow (:key => value) syntax.

Other than that, it's really up to personal preference. I prefer the colon syntax because it generally reduces visual clutter, by virtue of having one less symbol on the screen, without reducing readability.

One scenario I prefer the arrow syntax for, though, is in DSLs where a hash's arrow syntax is being used (or, it could be argued, abused) to represent a "from X to Y" relationship. A good example is the state_machine gem, which describes transitions like this:

transition :first_gear => :idling

Here the arrow makes it this read almost like English: "On idle, objects in the first gear state should transition to idling." It wouldn't be illegible with the colon syntax, but because state_machine also allows arrays as keys, it's good to use arrow syntax anyway just for consistency:

transition first_gear: :idling
# ...
transition [:idling, :first_gear] => :parked

Here they don't match, and it's a visually and mentally jarring—not much, but enough to be worth consideration.

I also sometimes prefer it in Rails when doing ActiveRecord queries through associations, e.g.

User.where( active: true, :role => { name: "admin" } )
# vs.
User.where( active: true, role: { name: "admin" } )

For some reason the => helps reinforce my understanding of the association or the JOIN. Once again, merely a personal preference, and in this case I think it makes even less of a difference than in the state_machine example.

I think it comes down to personal preference. Personally, I try to avoid using hashrockets as much as possible. In some situations, you are obligated to use a hashrocket, such as:

Post.joins(:comments).where('comments.commenter' => 'Peter')

The new syntax only supports symbols as keys, not strings. If you happen to use a lot of strings as keys the answer to your question clearly points to sticking with the hashrocket. However, in my personal experience I can use symbols as keys 9/10 of the time.

The new syntax, introduced by 1.9, seems more clean to me. It is hard to explain, but if I open a bunch of code with hashrockets and rewrite the code to use : instead it makes me so much happier. We could talk about tiny technical differences, but in the end it really comes down to what looks and feels better to you. For me it basically feels the same as with Coffeescript (altough this involves a lot more syntax changes). I just like the look of it in my editor.

One more small consideration. Personally I like to go with the newest syntax whenever possible. I wouldn't worry about deprecations, but it feels more natural to evolve with Rails. I don't like the feeling of using 'old' syntax, which is of course completely subjective.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top