在 Ruby 中,“=>”代表什么?是什么意思以及它是如何工作的?

发布于 2024-10-11 20:07:55 字数 222 浏览 9 评论 0原文

在学习 Ruby 时,我遇到了“=>”有时操作员。通常我会以 的形式看到它

:symbol => value

,并且在向函数传递值时似乎经常使用它。那个操作员到底叫什么名字?它有什么作用/意味着什么?它是内置在 Ruby 中还是由不同的框架(如 Rails 和 DataMapper)添加到符号类中?它只能与符号类一起使用吗?谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小耗子 2024-10-18 20:07:55

=> 将哈希映射文字中的键与值分开。它不可重载,也没有专门连接到符号。

哈希图文字的形式为 {key1 =>值1,键2 => value2, ...},但是当用作函数的最后一个参数时,可以省略花括号。因此,当您看到像 f(:a => 1, :b => 2) 这样的函数调用时,f 会使用一个参数进行调用,该参数是一个 hashmap具有键 :a:b 以及值 12

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

獨角戲 2024-10-18 20:07:55

您可能听说过这个运算符被称为“哈希火箭”,这意味着您在定义 ruby​​ 哈希时使用它。

如果您不熟悉,这是 Ruby Hash 文档:http://www.ruby-doc。 org/core/classes/Hash.html

请注意,在 Ruby 1.9 中,如果您要定义使用符号作为键的哈希,现在可以使用替代语法: http://blog.peepcode.com/tutorials/2011/rip-ruby-hash-rocket-syntax

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

宛菡 2024-10-18 20:07:55

提示:如果您在像 {:a =>; 这样的哈希中使用它“A”,:b => "B"},在 Ruby 1.9 中,您可以像 JSON 哈希一样使用它:

{
  a: "A",
  b: "B"
}

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"
}
睫毛溺水了 2024-10-18 20:07:55

如果您想进一步进行谷歌搜索,=> 有时被称为 hashrocket,因为它看起来像火箭(与 <=> 看起来像火箭一样)像宇宙飞船),并且它用于哈希。

或者您可以使用 SymbolHound

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.

国产ˉ祖宗 2024-10-18 20:07:55

除了 在 Ruby 中还有什么“ =>”含义及其工作原理?

您通常会看到 => 来定义函数的参数。将此视为一个很好的便利:您无需记住参数的正确顺序,因为所有参数都包装在一个巨大的散列中。因此,如果您有一个简单的辅助方法,例如

link_to "My link", my_path, :confirm => “你确定吗?”

这比

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

更好,只是因为你想使用很少使用的参数。因此,使用散列传递参数只是 Ruby/Rails 中的一种约定,以使生活更轻松。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文