如何使用新的 ruby​​ (1.9) 哈希语法从关联创建符号(哈希键)? [英] How to create symbol (hash key) from association, using new ruby (1.9) hash syntax?

查看:23
本文介绍了如何使用新的 ruby​​ (1.9) 哈希语法从关联创建符号(哈希键)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我什至不知道如何问这个问题..所以如果你也能提出建议,我们将不胜感激.

Sorry, I'm not even sure how to ask this question.. so if you could suggest around that as well, it would be appreciated.

# A. WORKS, but "stockroom" is hardcoded
render partial: association.to_s.singularize + '',
  locals: {stockroom: new_object}

# B. WORKS, but uses old syntax
render partial: association.to_s.singularize + '',
  locals: {association.to_s.singularize.to_sym => new_object}

# C. does NOT work
render partial: association.to_s.singularize + '',
  locals: {association.to_s.singularize.to_sym: new_object}

# D. does NOT work
ass = association.to_s.singularize.to_sym
logger.debug "--- ass: #{ass.inspect} (#{ass.class})"
  # => --- ass: :stockroom (Symbol)
render partial: association.to_s.singularize + '', locals: {ass: new_object}

并不是说旧语法是坏事,我只是想知道是否有办法使用新语法(甚至通过中介(即屁股))来做到这一点.

Not that the old syntax is a bad thing, I'm just wondering if there's a way to do this using the new syntax (even via an intermediary (i.e. ass)).

推荐答案

JavaScript 风格的语法并不是真正的 语法,它只是 1.9 中的一个替代语法可以用于哈希文字中的一些符号文字.

The JavaScript style syntax is not really the new syntax, it is just an alternate syntax in 1.9 that can be used for some symbol literals within a Hash literal.

您不能对所有符号使用尾随冒号样式,试试这些,您会看到:

You can't use the trailing-colon style with all symbols, try these and you'll see:

{ :$set => 11 }                       # Valid
{ $set: 11 }                          # Invalid
{ :'where.is.pancakes.house?' => 23 } # Valid
{ 'where.is.pancakes.house?': 23 }    # Invalid

同样,如果您有一个不是符号文字的符号:

Similarly, if you have a symbol that isn't a symbol literal:

s = o.to_sym; { s => 42 }
{ o.to_sym => 42 }

那么你必须在你的哈希中使用粗箭头语法.如果您的密钥不是符号,则同样适用:

then you have to use the fat arrow syntax in your Hashes. The same applies if you have a key that isn't a symbol:

a = %w{where is pancakes house?}
h = { a => 11 }
g = { a: 11 } # Also works but produces a completely different result!

总而言之,尾随冒号转换 some 文字值(我认为任何匹配 /A[a-z_]w*z/i 但我不确定)在哈希文字中使用时,它位于符号之前.基本上,除了最琐碎的情况外,JavaScript 风格在所有情况下都是无用的;但最常见的却是小事.

In summary, the trailing-colon converts some literal values (I think anything that matches /A[a-z_]w*z/i but I'm not certain) that precede it to symbols when used inside a Hash literal. Basically, the JavaScript style is useless in all but the most trivial cases; but the trivial cases do happen to be the most common.

我能想到的将 JavaScript 样式与非文字符号一起使用的唯一方法是这样的可憎:

The only way I can think of to use the JavaScript style with a non-literal symbol would be an abomination like this:

ass = association.to_s.singularize.to_sym
h   = eval "{ #{ass}: 'pancakes' }"

如果你曾经在真正的代码中写过类似的东西,那么愿众神怜悯你的灵魂.

and if you ever put something like that in real code then may the gods have mercy on your soul.

另外,请记住,当您要访问哈希时,必须使用符号的前导冒号形式:

Also, keep in mind that you have to use the leading-colon form of the symbol when you want to access your hash:

h[:pancakes] # Valid
h[pancakes:] # Invalid

所以 JavaScript 风格的实用性有限.

so the JavaScript style is of limited utility.

这篇关于如何使用新的 ruby​​ (1.9) 哈希语法从关联创建符号(哈希键)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆