[ACCEPTED]-Ruby on Rails: what does the => symbol mean?-ruby-on-rails

Accepted answer
Score: 22

I've heard it commonly referred to as a 9 "hash rocket". It is the assignment operator 8 used with hashes in ruby. So if you have 7 a hash and want to assign a value to a key 6 (typically a literal), use

{key1 => value1, key2 => value2}

Rails, and other 5 Ruby code, often pass hashes as parameters 4 to methods to achieve the same effect as 3 named arguments in other languages like 2 Python.

object.method({:param1 => value1, :param2 => value2})

EDIT: When reading, I use "gets" as 1 the verb, eg. param1 gets value1, etc.

Score: 12

Your first function call is a shortcut for

map.connect('/marmots/new', {:controller=>'marmots', :action=>'new'})

where 7 the {} are a Hash-literal. The second argument 6 of the method connect of the object map 5 is an object of class Hash with the two 4 keys :controller and :action (both are literals 3 of the class Symbol) whose corresponding 2 values are the two strings 'marmots' and 1 'new'.

EDIT: I call it "arrow" or "maps to".

More Related questions