在哪里使用 ruby splat 运算符是合法的?
泼溅很酷。 它们不仅仅用于爆炸数组,尽管这很有趣。 它们还可以转换为数组并展平数组(请参阅http://github.com/mischa/splat/ tree/master 获取其功能的详尽列表。)
看起来无法在 splat 上执行其他操作,但在 1.8.6/1.9 中,以下代码会抛出“意外的 tSTAR”:
foo =酒吧|| *扎普#=> 意外的 tSTAR
而这有效:
foo = *zap || 栏#=> 有效,但价值有限
splat 可以出现在表达式中的什么位置?
Splats are cool. They're not just for exploding arrays, although that is fun. They can also cast to Array and flatten arrays (See http://github.com/mischa/splat/tree/master for an exhaustive list of what they do.)
It looks like one cannot perform additional operations on the splat, but in 1.8.6/1.9 the following code throws "unexpected tSTAR":
foo = bar || *zap #=> unexpected tSTAR
Whereas this works:
foo = *zap || bar #=> works, but of limited value
Where can the splat appear in an expression?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,优先级在这里不是问题,因为
foo = bar || (*zap)
效果也好不到哪儿去。 一般经验法则是您不能对 splat 执行其他操作。 即使像foo = (*zap)
这样简单的东西也是无效的。 这也适用于 1.9。话虽如此,您期望什么
foo = bar || *zap
去做,如果有效的话,这与 foo = bar || 不同 扎普? 即使在像 a, b = bar || 这样的情况下 *zap (这也不起作用),a, b = bar || zap
完成了我认为是相同的事情。唯一有意义的情况是像
a, b = foo, bar || 这样的情况。 *扎普
。 您应该发现您想要使用它的大多数情况都包含在a, b = foo, *(bar || zap)
中。 如果这不能满足您的情况,您可能应该问自己,通过编写如此丑陋的构造,您真正希望实现什么目的。编辑:
为了回应您的评论,
*zap || bar
相当于*(zap || bar)
。 这表明了 splat 的优先级有多低。 到底有多低? 我能给你的最好答案是“相当低”。不过,举一个有趣的例子,考虑一个方法
foo
,它接受三个参数:foo(*bar = [1, 2, 3])
将在赋值后弹出并设置分别为 1、2 和 3 的参数。 与foo((*bar = [1, 2, 3]))
相比,它会抱怨参数数量错误(1 代表 3)。First, precedence isn't an issue here, because
foo = bar || (*zap)
works no better. The general rule of thumb is that you cannot perform additional operations on a splat. Even something as simple asfoo = (*zap)
is invalid. This applies to 1.9 as well.Having said that, what do you expect
foo = bar || *zap
to do, if it worked, that is different thanfoo = bar || zap
? Even in a case likea, b = bar || *zap
(which also doesn't work),a, b = bar || zap
accomplishes what I'd assume would be the same thing.The only situation where this might make any sense is something like
a, b = foo, bar || *zap
. You should find that most cases where you would want to use this are covered bya, b = foo, *(bar || zap)
. If that doesn't cover your case, you should probably ask yourself what you really hope to accomplish by writing such an ugly construct.EDIT:
In response to your comments,
*zap || bar
is equivalent to*(zap || bar)
. This demonstrates how low the splat's precedence is. Exactly how low is it? The best answer I can give you is "pretty low".For an interesting example, though, consider a method
foo
which takes three arguments:foo(*bar = [1, 2, 3])
will splat after the assignment and set the arguments to 1, 2, and 3 respectively. Compare that withfoo((*bar = [1, 2, 3]))
which will complain about having the wrong number of arguments (1 for 3).“splat 运算符”实际上根本不是一个运算符,而是 Ruby 语法中定义的一个标记。 通读 Grammar.y 或 BNF 形式* 的 Ruby 语法会告诉您,它可以作为最后一个或唯一的参数:
&foo
除外&foo
除外)a, b, *cs = [1,2,3,4]< /code>
a, b, c = 1, 2, *[3,4,5]
The "splat operator" is in fact not an operator at all but a token defined in the Ruby grammar. A read through grammar.y or the Ruby grammar in BNF form* will tell you that it is allowed as the last or only argument:
&foo
)&foo
)a, b, *cs = [1,2,3,4]
a, b, c = 1, 2, *[3,4,5]