使用ruby splat操作符在哪里合法?

时间:2022-06-21 00:22:36

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.)

长条木板很酷。它们不只是用于爆炸数组,尽管这很有趣。它们还可以对数组和flatten Array(参见http://github.com/mischa/splat/tree/master)。

It looks like one cannot perform additional operations on the splat, but in 1.8.6/1.9 the following code throws "unexpected tSTAR":

看起来一个人不能在splat上执行额外的操作,但是在1.8.6/1.9中,以下代码抛出了“意外的tSTAR”:

foo = bar || *zap #=> unexpected tSTAR

foo = bar || *zap #=>意外tSTAR

Whereas this works:

而如此:

foo = *zap || bar #=> works, but of limited value

foo = *zap || bar #=>有效,但值有限

Where can the splat appear in an expression?

表达式中的splat在哪里?

2 个解决方案

#1


15  

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.

首先,这里不存在优先级问题,因为foo = bar || (*zap)没有更好的效果。一般的经验是,您不能对splat执行其他操作。即使是像foo = (*zap)这样简单的东西也是无效的。这也适用于1.9。

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.

说到这里,你希望foo = bar || *zap做什么,如果它有效,那和foo = bar || zap有什么不同?即使在a, b = bar || *zap(这也没用)的情况下,a, b = bar || zap完成了我假设的相同的事情。

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.

唯一有意义的情况是a b = foo bar || *zap。您应该会发现,您想要使用它的大多数情况都包含在a、b = foo、*(bar || zap)中。如果这还不能涵盖你的情况,你应该问问自己,你真正希望通过编写这样一个丑陋的结构来实现什么。


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".

针对您的评论,*zap || bar等于*(zap || bar)。这显示了splat的优先级有多低。到底有多低?我能给你的最好答案是“相当低”。

For an interesting example, though, consider a method foo which takes three arguments:

但是,对于一个有趣的示例,请考虑一个方法foo,它包含三个参数:

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).

foo(*bar =[1,2,3])将在赋值后分裂,并分别将参数设置为1,2,3。与foo(*bar =[1,2,3])相比,它会抱怨参数的数量错误(1代表3)。

#2


8  

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:

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

  • in a method definition (except for an optional last &foo)
  • 在方法定义中(除了可选的last &foo)
  • in a method call (except for an optional last &foo)
  • 在方法调用中(除了可选的last &foo)
  • on the LHS of as assignment, for example: a, b, *cs = [1,2,3,4]
  • 在as赋值的LHS上,例如:a, b, *cs = [1,2,3,4]
  • on the RHS of an assignment, for example: a, b, c = 1, 2, *[3,4,5]
  • 在作业的RHS中,例如:a、b、c = 1、2、*[3、4、5]
  • in the when clause of a case statement
  • 在案件陈述的when子句中

#1


15  

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.

首先,这里不存在优先级问题,因为foo = bar || (*zap)没有更好的效果。一般的经验是,您不能对splat执行其他操作。即使是像foo = (*zap)这样简单的东西也是无效的。这也适用于1.9。

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.

说到这里,你希望foo = bar || *zap做什么,如果它有效,那和foo = bar || zap有什么不同?即使在a, b = bar || *zap(这也没用)的情况下,a, b = bar || zap完成了我假设的相同的事情。

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.

唯一有意义的情况是a b = foo bar || *zap。您应该会发现,您想要使用它的大多数情况都包含在a、b = foo、*(bar || zap)中。如果这还不能涵盖你的情况,你应该问问自己,你真正希望通过编写这样一个丑陋的结构来实现什么。


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".

针对您的评论,*zap || bar等于*(zap || bar)。这显示了splat的优先级有多低。到底有多低?我能给你的最好答案是“相当低”。

For an interesting example, though, consider a method foo which takes three arguments:

但是,对于一个有趣的示例,请考虑一个方法foo,它包含三个参数:

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).

foo(*bar =[1,2,3])将在赋值后分裂,并分别将参数设置为1,2,3。与foo(*bar =[1,2,3])相比,它会抱怨参数的数量错误(1代表3)。

#2


8  

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:

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

  • in a method definition (except for an optional last &foo)
  • 在方法定义中(除了可选的last &foo)
  • in a method call (except for an optional last &foo)
  • 在方法调用中(除了可选的last &foo)
  • on the LHS of as assignment, for example: a, b, *cs = [1,2,3,4]
  • 在as赋值的LHS上,例如:a, b, *cs = [1,2,3,4]
  • on the RHS of an assignment, for example: a, b, c = 1, 2, *[3,4,5]
  • 在作业的RHS中,例如:a、b、c = 1、2、*[3、4、5]
  • in the when clause of a case statement
  • 在案件陈述的when子句中