ruby `:key => "value"` 和 `key: "value"` 哈希符号之间有什么区别吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8675206/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:38:15  来源:igfitidea点击:

Is there any difference between the `:key => "value"` and `key: "value"` hash notations?

rubysyntaxhashruby-1.9hashrocket

提问by AdamNYC

Is there any difference between :key => "value"(hashrocket) and key: "value"(Ruby 1.9) notations?

:key => "value"(hashrocket) 和key: "value"(Ruby 1.9) 符号之间有什么区别吗?

If not, then I would like to use key: "value"notation. Is there a gem that helps me to convert from :x =>to x:notations?

如果没有,那么我想使用key: "value"符号。是否有宝石可以帮助我转换:x =>x:符号?

回答by mu is too short

Yes, there is a difference. These are legal:

是,有一点不同。这些是合法的:

h = { :$in => array }
h = { :'a.b' => 'c' }
h[:s] = 42

but these are not:

但这些不是:

h = { $in: array }
h = { 'a.b': 'c' } # but this is okay in Ruby2.2+
h[s:] = 42

You can also use anything as a key with =>so you can do this:

您还可以使用任何东西作为键,=>以便您可以这样做:

h = { C.new => 11 }
h = { 23 => 'pancakes house?' }

but you can't do this:

但你不能这样做:

h = { C.new: 11 }
h = { 23: 'pancakes house?' }

The JavaScript style (key: value) is only useful if all of your Hash keys are "simple" symbols (more or less something that matches /\A[a-z_]\w*\z/i, AFAIK the parser uses its label pattern for these keys).

JavaScript 样式 ( key: value) 仅在您的所有 Hash 键都是“简单”符号(或多或少匹配的东西/\A[a-z_]\w*\z/i,AFAIK 解析器对这些键使用其标签模式)时才有用。

The :$instyle symbols show up a fair bit when using MongoDB so you'll end up mixing Hash styles if you use MongoDB. And, if you ever work with specific keys of Hashes (h[:k]) rather than just whole hashes (h = { ... }), you'll still have to use the colon-first style for symbols; you'll also have to use the leading-colon style for symbols that you use outside of Hashes. I prefer to be consistent so I don't bother with the JavaScript style at all.

:$in使用 MongoDB 时,样式符号会显示出相当多的内容,因此如果您使用 MongoDB,最终会混合 Hash 样式。而且,如果您曾经使用过 Hashes ( h[:k]) 的特定键,而不仅仅是整个哈希 ( h = { ... }),您仍然必须对符号使用冒号优先样式;对于在哈希之外使用的符号,您还必须使用前导冒号样式。我更喜欢保持一致,所以我根本不关心 JavaScript 风格。

Some of the problems with the JavaScript-style have been fixed in Ruby 2.2. You can now use quotes if you have symbols that aren't valid labels, for example:

JavaScript 风格的一些问题已在 Ruby 2.2 中得到修复。如果您的符号不是有效标签,您现在可以使用引号,例如:

h = { 'where is': 'pancakes house?', '$set': { a: 11 } }

But you still need the hashrocket if your keys are not symbols.

但是如果你的键不是符号,你仍然需要 hashrocket。

回答by ranksrejoined

key: "value"is a convenience feature of Ruby 1.9; so long as you know your environment will support it, I see no reason not to use it. It's just much easier to type a colon than a rocket, and I think it looks much cleaner. As for there being a gem to do the conversion, probably not, but it seems like an ideal learning experience for you, if you don't already know file manipulation and regular expressions.

key: "value"是 Ruby 1.9 的一个便利特性;只要您知道您的环境会支持它,我就没有理由不使用它。打一个冒号比打一个火箭要容易得多,而且我认为它看起来更干净。至于有一个 gem 来进行转换,可能不会,但如果您还不了解文件操作和正则表达式,这对您来说似乎是一种理想的学习体验。

回答by alexanderjsingleton

Ruby hash-keys assigned by hash-rockets can facilitate strings for key-value pairs (e.g. 's' => x) whereas key assignment via symbols(e.g.key: "value"or :key => "value") cannot be assigned with strings.Although hash-rockets provide freedom and functionality for hash-tables, specifically allowing strings as keys, application performance may be slower than if the hash-tables were to be constructed with symbols as hash-keys. The following resources may be able to clarify any differences between hashrockets and symbols:

由 hash-rockets 分配的 Ruby 哈希键可以促进键值对(例如. 's' => x)的字符串,而通过符号例如key: "value":key => "value")分配的键不能用字符串分配。尽管哈希火箭为哈希表提供了自由度和功能,特别是允许字符串作为键,但应用程序性能可能比使用符号作为哈希键构造哈希表慢。以下资源可能能够阐明 hashrockets 和符号之间的任何差异:

回答by sczizzo

The key: valueJSON-style assignments are a part of the new Ruby 1.9 hash syntax, so bear in mind that this syntax will not work with older versions of Ruby. Also, the keys are going to be symbols. If you can live with those two constraints, new hashes work just like the old hashes; there's no reason (other than style, perhaps) to convert them.

key: valueJSON风格的任务是新的Ruby 1.9哈希语法的一部分,所以要记住,这句法不会与旧版本的Ruby的工作。此外,键将是符号。如果你能忍受这两个约束,新的散列就像旧的散列一样工作;没有理由(也许除了风格)转换它们。

回答by Charles

Doing :key => valueis the same as doing key: value, and is really just a convenience. I haven't seen other languages that use the =>, but others like Javascript use the key: valuein their Hash-equivalent datatypes.

:key => value和做一样key: value,实际上只是一种方便。我还没有看到其他语言使用=>,但其他语言(如 Javascript)key: value在其哈希等效数据类型中使用 。

As for a gem to convert the way you wrote out your hashes, I would just stick with the way you are doing it for your current project.

至于转换你写出哈希的方式的 gem,我会坚持你当前项目的方式。

*Note that in using key: valuethe key will be a symbol, and to access the value stored at that key in a foohash would still be foo[:key].

*请注意,在使用key: value密钥时将是一个符号,而要访问存储在该密钥中的foo哈希值仍然是foo[:key].