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

查看:106
本文介绍了如何使用新的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}

不是旧的语​​法是坏事,我只是想知道是否有一种方法可以使用新的语法(甚至通过中介(即屁股))来执行此操作。 / style>

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风格的语法不是真正的 new 语法,它只是一个在1.9中

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 }

然后你必须在你的Hashes中使用胖箭头语法。如果您有一个不是符号的钥匙,情况也是如此:

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!

综上所述,尾部分号转换了一些字面值(我认为任何符合 / \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天全站免登陆