The meaning of => in ruby

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)f is called with one argument, which is a hashmap that has the keys :aand :b and the values 1 and 2.

 

posted on 2013-11-23 15:29  优雅的码农  阅读(110)  评论(0)    收藏  举报

导航