Ruby on Rails: what does the => symbol mean?

前端 未结 2 1155
执笔经年
执笔经年 2021-02-01 20:22

I am working my way through Head First Rails, and I keep seeing =>. It\'s in the routes:

map.connect \'/marmots/new\', controller=>\'marmots\',

2条回答
  •  南旧
    南旧 (楼主)
    2021-02-01 21:08

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

    {key1 => value1, key2 => value2}
    

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

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

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

提交回复
热议问题