While learning Ruby I’ve come across the “=>” operator on occasion. Usually I see it in the form of
:symbol => value
and it seems to be used frequently when passing values to functions. What exactly is that operator called? What does it do/mean? Is it built into Ruby or is it something that different frameworks like Rails and DataMapper add to the symbol class? Is it only used in conjunction with the symbol class? Thanks.
=>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 likef(:a => 1, :b => 2),fis called with one argument, which is a hashmap that has the keys:aand:band the values1and2.