在 Ruby 中,“=>”是什么意思,它是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4663074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Ruby what does "=>" mean and how does it work?
提问by Dustin Martin
While learning Ruby I've come across the "=>" operator on occasion. Usually I see it in the form of
在学习 Ruby 时,我偶尔会遇到“=>”运算符。通常我看到它的形式
:symbol => value
and it seemsto 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.
并且在将值传递给函数时似乎经常使用它。那个运算符到底叫什么?它的作用是什么?它是内置于 Ruby 中还是由不同的框架(如 Rails 和 DataMapper)添加到符号类中的?它是否仅与符号类结合使用?谢谢。
回答by sepp2k
=>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), fis called with one argument, which is a hashmap that has the keys :aand :band the values 1and 2.
哈希映射文字的形式为{key1 => value1, key2 => value2, ...},但是当用作函数的最后一个参数时,您可以省略大括号。因此,当您看到像f(:a => 1, :b => 2), 之类的函数调用时,f它带有一个参数,该参数是一个具有键:a和:b值1和值的哈希图2。
回答by jmcnevin
You might hear this operator referred to as a "hash rocket," meaning you use it when defining a ruby hash.
您可能会听到这个运算符被称为“哈希火箭”,这意味着您在定义 ruby 哈希时会使用它。
This is the Ruby Hash documentation, if you're not familiar: http://www.ruby-doc.org/core/classes/Hash.html
如果您不熟悉,这是 Ruby Hash 文档: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
请注意,在 Ruby 1.9 中,如果您要定义使用符号作为键的哈希,则现在可以使用替代语法:http: //blog.peepcode.com/tutorials/2011/rip-ruby-hash-rocket-句法
回答by amrnt
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"}在 Ruby 1.9 之类的散列中使用它,则可以像使用 JSON 散列一样使用它:
{
a: "A",
b: "B"
}
回答by Andrew Grimm
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.
如果你想进一步使用谷歌搜索,=>有时被称为 hashrocket,因为它看起来像一个火箭(与<=>看起来像一艘宇宙飞船的意义相同),并且它用于哈希。
Or you could use SymbolHound.
或者你可以使用SymbolHound。
回答by d135-1r43
In addition to In Ruby what does "=>" mean and how does it work?:
除了在 Ruby 中,“=>”是什么意思,它是如何工作的?:
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?"
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?"
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.
只是因为您想使用很少使用的参数。因此,使用散列传递参数只是 Ruby/Rails 中的一种约定,以使生活更轻松。

