在哪里使用 ruby​​ splat 运算符是合法的?

发布于 2024-07-18 05:14:16 字数 408 浏览 13 评论 0原文

泼溅很酷。 它们不仅仅用于爆炸数组,尽管这很有趣。 它们还可以转换为数组并展平数组(请参阅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 技术交流群。

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

发布评论

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

评论(2

夕嗳→ 2024-07-25 05:14:16

首先,优先级在这里不是问题,因为 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 ,它接受三个参数:

def foo(a, b, c)
  #important stuff happens here!
end

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 as foo = (*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 than foo = bar || zap? Even in a case like a, 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 by a, 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:

def foo(a, b, c)
  #important stuff happens here!
end

foo(*bar = [1, 2, 3]) will splat after the assignment and set the arguments to 1, 2, and 3 respectively. Compare that with foo((*bar = [1, 2, 3])) which will complain about having the wrong number of arguments (1 for 3).

乞讨 2024-07-25 05:14:16

“splat 运算符”实际上根本不是一个运算符,而是 Ruby 语法中定义的一个标记。 通读 Grammar.y 或 BNF 形式* 的 Ruby 语法会告诉您,它可以作为最后一个或唯一的参数:

  • 在方法定义中(可选的最后一个 &foo 除外
  • ) 方法调用(可选的最后一个 &foo 除外)
  • 对 as 赋值的 LHS 进行 ,例如:a, b, *cs = [1,2,3,4]< /code>
  • 位于赋值的右侧,例如:
  • case 语句的 when 子句中的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:

  • in a method definition (except for an optional last &foo)
  • in a method call (except for an optional last &foo)
  • on the LHS of as assignment, for example: a, b, *cs = [1,2,3,4]
  • on the RHS of an assignment, for example: a, b, c = 1, 2, *[3,4,5]
  • in the when clause of a case statement
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文