← Back to Ruby Operators

Hash Rocket

The => symbol in the Ruby community is affectionately referred to as the hash rocket (maybe hashrocket too, though not to be confused with the consultancy).

Defining a hash in Ruby used to require the following syntax:

> {:ruby => :rails}
=> {:ruby=>:rails}

However, in 2007 with the release of Ruby 1.9, we got the colon syntax for symbol keys. Though this makes the hash rocket syntax disappear from a lot of code, we can still see it in IRB's response.

> {ruby: :rails}
=> {:ruby=>:rails}

We still need to use the hash rocket when defining non-symbol keys. If the colon is used with a string in a hash definition, the string will be coerced to a symbol.

> {"taco" => "bell"}
=> {"taco"=>"bell"}
> {"taco": "bell"}
=> {:taco=>"bell"}

The hash rocket syntax is required for any other kind of non-symbol key.

> { ['c',3] => 'Knight' }
=> {["c", 3]=>"Knight"}

References