In Ruby what does "=>" mean and how does it work? [duplicate] In Ruby what does "=>" mean and how does it work? [duplicate] ruby ruby

In Ruby what does "=>" mean and how does it work? [duplicate]


=> separates the keys from the values in a hashmap literal. It is not overloadable and not specifically connected to symbols.

A hashmap literal has the form {key1 => value1, key2 => value2, ...}, but when used as the last parameter of a function, you can leave off the curly braces. So when you see a function call like f(:a => 1, :b => 2), f is called with one argument, which is a hashmap that has the keys :a and :b and the values 1 and 2.


You might hear this operator referred to as a "hash rocket," meaning you use it when defining a ruby hash.

This is the Ruby Hash documentation, if you're not familiar: http://www.ruby-doc.org/core/classes/Hash.html

Note that in Ruby 1.9, if you're defining a hash that uses symbols as keys, there's now an alternative syntax available to you: http://blog.peepcode.com/tutorials/2011/rip-ruby-hash-rocket-syntax


Tip: if you're using it in a hash like {:a => "A", :b => "B"}, in Ruby 1.9, you can use it like a JSON hash:

{  a: "A",  b: "B"}