这是哪里的法律使用红宝石图示运营商? [英] Where is it legal to use ruby splat operator?

查看:209
本文介绍了这是哪里的法律使用红宝石图示运营商?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提示图标都很酷。他们不只是爆炸阵列,尽管这是乐趣。他们还可以转换为数组和扁平阵列(请参见 http://github.com/mischa/splat/tree/master为他们做什么的详尽清单。)

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

它看起来像一个人不能在图示执行其他操作,但在1.8.6 / 1.9以下code抛出意外tSTAR

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

富=酒吧|| * ZAP#=>意外tSTAR

而这个作品:

富= * ZAP ||酒吧#=>工作,但价值有限

在哪里可以提示图标出现在一个前pression?

Where can the splat appear in an expression?

推荐答案

首先,precedence是不是一个问题在这里,因为富=酒吧|| (* ZAP)工作没有更好的。一般的经验法则是,你不能在图示执行其他操作。即使是一些如 = foo的简单(* ZAP)是无效的。这适用于1.9为好。

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.

说了这么多,你能指望什么富=酒吧|| * ZAP 做的,如果它的工作,比富=酒吧不同|| ZAP ?即使是在像 A,B =酒吧案例|| * ZAP (这也不起作用), A,B = ||吧ZAP 完成什么,我会认为是一样的。

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.

在哪里,这可能使任何意义,唯一的情况是一样的东西 A,B = foo的,酒吧|| * ZAP 。你会发现,在这里,你会想用这个大多数情况下是由覆盖A,B = foo,那么*(巴|| 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.


在回答您的意见, * ZAP ||酒吧等同于 *(ZAP ||吧)。这表明图示的precedence有多低。究竟有多低呢?最好的答案,我可以给你的是pretty低。

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

美孚(*巴= [1,2,3])在转让之后将图示和参数分别设置为1,2和3。相比之下,与美孚((*巴= [1,2,3]))这会抱怨具有错误的参数数目(1 3)。

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

这篇关于这是哪里的法律使用红宝石图示运营商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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