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

5

58

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.

Indies answered 11/1, 2011 at 21:56 Comment(0)
D
62

=> 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.

Danas answered 11/1, 2011 at 21:57 Comment(4)
For more reading, see the Pickaxe tutorial section on Hashes and reference section on Hashes. Note that in Ruby 1.9 you can alternatively specify a literal key/value pair in a Hash like this foo: bar, which creates the key as the Symbol :foo.Cab
Break large problems into smaller ones. If you get to know associative arrays and ruby Symbols, you won't have any problem understand Hashes.Shotwell
So just to clarify, here is an example from DataMapper property :title, String, :required => true, :length => 5..200 This is the same as property(:title, String, {:required => true, :length => 5..200}) ?Indies
But we can also use this => with rescue ... For example begin ... rescue SignalException, Interrupt, SystemExit => e...endFoothold
R
15

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

Rosenstein answered 11/1, 2011 at 22:30 Comment(0)
A
7

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"
}
Arin answered 11/1, 2011 at 23:1 Comment(0)
R
5

If you want to do any further Googling, => is sometimes called a hashrocket, because it looks like a rocket (in the same sense that <=> looks like a spaceship), and it's used in hashes.

Or you could use SymbolHound.

Rooney answered 11/1, 2011 at 22:31 Comment(1)
@DustinMartin I've recently come across a search engine called SymbolHound, in case you have difficulty searching for other syntax.Rooney
F
-1

In addition to In Ruby what does "=>" mean and how does it work?:

You mostly will see the => to define parameters for a function. Think of this as a nice convenience: You need not remember the right order of your parameters, as all parameters are wrapped into a giant hash. So if you have a simple helper method like

link_to "My link", my_path, :confirm => "Are you sure?"

this is way better than

link_to "My link", my_path, null, null, null, null, "Are you sure?"

just because you want to use a rarely used parameter. So passing parameters with a hash is just a convention in Ruby/Rails to make life easier.

Forjudge answered 12/1, 2011 at 16:12 Comment(1)
it's also wrong as you would just use link_to "My link", my_path, confirm: "Are you sure?"Quorum

© 2022 - 2024 — McMap. All rights reserved.