标记未使用的块变量 [英] Marking an unused block variable
问题描述
当存在不使用的块或局部变量时,有时人们用*
标记,有时用_
标记.
When there is a block or local variable that is not to be used, sometimes people mark it with *
, and sometimes with _
.
{[1, 2] => 3, [4, 5] => 6}.each{|(x, *), *| p x}
{[1, 2] => 3, [4, 5] => 6}.each{|(x, _), _| p x}
{[1, 2, 3], [4, 5, 6]}.each{|*, x, *| p x}
{[1, 2, 3], [4, 5, 6]}.each{|_, x, _| p x}
def (x, *), *; p x; end
def (x, _), _; p x; end
def *, x, *; p x; end
def _, x, _; p x; end
它们之间有什么区别,什么时候应该使用?如上例所示,当需要将多个变量标记为未使用时,还是更好?
What are the differences between them, and when should I use which? When there is need to mark multiple variables as unused as in the above examples, is either better?
推荐答案
*
表示所有剩余参数". _
只是另一个变量名,尽管有点特殊.因此它们是不同的,例如以下内容是没有道理的:
A *
means "all remaining parameters". An _
is just another variable name, although it is a bit special. So they are different, for example the following does not make sense:
[[1, 2, 3], [4, 5, 6]].each{|*, x, *| p x} # Syntax error
实际上,Ruby应该如何知道第一颗星应该获得0、1或2个值(以及相反的值)?
Indeed, how is Ruby supposed to know if the first star should get 0, 1 or 2 of the values (and the reverse)?
在极少数情况下,您想使用星号来忽略参数.例如,如果您只想使用可变数量的参数中的最后一个:
There are very few cases where you want to use a star to ignore parameters. An example would be if you only want to use the last of a variable number of parameters:
[[1], [2, 3], [4, 5, 6]].each{|*, last| p last} # => prints 1, 3 and 6
Ruby允许您不给参数的其余部分"起个名字,但是您可以使用_
:
Ruby allows you to not give a name to the "rest" of the parameters, but you can use _
:
[[1], [2, 3], [4, 5, 6]].each{|*_, last| p last} # => prints 1, 3 and 6
通常,参数的数量是已知的,最好的选择是使用_
:
Typically, the number of parameters is known and your best choice is to use a _
:
[[1, 2, 3], [4, 5, 6]].each{|_, mid, _| p mid} # prints 2 and 5
请注意,您也可以不给最后一个参数命名(就像使用*
时一样),尽管它不太明显:
Note that you could leave the last paramater unnamed too (like you can when using a *
), although it is less obvious:
[[1, 2, 3], [4, 5, 6]].each{|_, mid, | p mid} # prints 2 and 5
现在_
是 指定变量名称,当您不想使用值时使用.它是一个特殊的变量名,原因有二:
Now _
is the designated variable name to use when you don't want to use a value. It is a special variable name for two reasons:
- 如果您不使用Ruby(如果警告已打开),它将不会抱怨
- Ruby将允许您在参数列表中重复它.
第1点的示例
> ruby -w -e "def foo; x = 42; end; foo"
-e:1: warning: assigned but unused variable - x
> ruby -w -e "def foo; _ = 42; end; foo"
no warning
第2点示例:
[[1, 2, 3], [4, 5, 6]].each{|unused, mid, unused| p mid}
# => SyntaxError: (irb):23: duplicated argument name
[[1, 2, 3], [4, 5, 6]].each{|_, mid, _| p mid}
# => prints 2 and 5
最后,如@DigitalRoss所述,_
将最后一个结果保存在irb
Finally, as @DigitalRoss notes, _
holds the last result in irb
更新:在Ruby 2.0中,您可以使用以_
开头的任何变量表示未使用.这样,变量名可以更明确地说明被忽略的内容:
Update: In Ruby 2.0, you can use any variable starting with _
to signify it is unused. This way the variable name can be more explicit about what is being ignored:
_scheme, _domain, port, _url = parse_some_url
# ... do something with port
这篇关于标记未使用的块变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!