在块参数中,下划线(_)代表占位符。

huangapple go评论58阅读模式
英文:

What does a _ mean in block arguments?

问题

我想知道有没有人可以解释一下破折号占位符的作用?

def remove_every_other(arr)
  arr.select.with_index { |_,idx| idx.even? }
end
英文:

I was wondering if somebody could please explain to me what the dash place holder does?

def remove_every_other(arr)
  arr.select.with_index { |_,idx| idx.even? }
end

答案1

得分: 7

在某种程度上,这只是像任何其他参数一样的另一个参数。参数名为_和参数名为idx之间没有区别。它们都是像任何其他参数一样的参数。

所以,在某种程度上,_没有什么特殊之处:它只是像任何其他名称一样的名称,也可以叫做foobarbaz

然而,这并不完全正确:Ruby语言确实对_有特殊处理。更准确地说,任何以_开头的局部变量名或参数名都受到Ruby语言的特殊对待。

在许多社区中,标识符_或以_开头的标识符通常用来表示某些内容被有意忽略。这也是Ruby社区中的用法。

为了支持将_用于被忽略的变量或参数的用法,Ruby语言以两种方式特殊对待以_开头的局部变量和参数:

未使用的局部变量警告

Ruby会为未使用的局部变量生成警告:

a = 42
b = 42

b

将生成警告:"warning: assigned but unused variable - a"。

然而,如果我将变量重命名为_a_,警告将被抑制:

_a = 42
_ = 42
b = 42

b

重复参数SyntaxError

在相同参数列表中两次使用相同的名称(在方法参数列表、块参数列表和lambda文字参数列表中)会引发SyntaxError

def foo(a, a) end
# duplicated argument name (SyntaxError)
foo {|a, a|}
# duplicated argument name (SyntaxError)
-> (a, a) {}
# duplicated argument name (SyntaxError)

_a_是有效的:

def foo(_a, _a, _, _) end
foo {|_a, _a, _, _|}
-> (_a, _a, _, _) {}

IRb中的最后结果

还有一个*_在IRb中特别使用的_用法,与上述内容无关:在IRb中,本地变量_会自动分配上次在当前IRb会话中评估的表达式的值。但这纯粹是IRb的一个特性,与Ruby无关。

在检查工具/样式检查器/静态分析器中的处理

许多检查工具、样式检查器和静态分析器的默认规则集禁止未使用的参数和局部变量,因为这通常表示拼写错误、编程错误或重构中的遗留代码。与Ruby本身类似,它们通常允许_开头的变量和参数。

然而,这与Ruby语言本身无关,而是Ruby社区的事情,更准确地说,是那些遵循社区内多数用法的工具开发人员的事情。

英文:

In some sense, this is just another parameter like any other. There is no difference between the parameter named _ and the parameter named idx. They are both parameters like any other parameter.

So, in some sense, there is nothing special about _: it's a name like any other name and could just as well be named foo or bar or baz.

However, that is not quite true: _ is treated special by the Ruby language. More precisely, any local variable name or parameter name starting with _ is treated special by the Ruby language.

In many communities, the identifier _ or identifiers starting with _ are used to signify that something is being deliberately ignored. And this is also the usage in the Ruby community.

In order to support this usage of _ for ignored variables or parameters, the Ruby language treats local variables and parameters starting with _ special in two ways:

Unused local variable warnings

Ruby will generate warnings for unused local variables:

a = 42
b = 42

b

will generate "warning: assigned but unused variable - a".

However, if I rename the variable to _a or _, the warning will be suppressed:

_a = 42
_ = 42
b = 42

b

Duplicate parameter SyntaxError

Using the same name twice in the same parameter list (in a method parameter list, block parameter list, and lambda literal parameter list) is a SyntaxError:

def foo(a, a) end
# duplicated argument name (SyntaxError)
foo {|a, a|}
# duplicated argument name (SyntaxError)
-> (a, a) {}
# duplicated argument name (SyntaxError)

but _a or _ are valid:

def foo(_a, _a, _, _) end
foo {|_a, _a, _, _|}
-> (_a, _a, _, _) {}

Last result in IRb

There is a third usage of _ specifically in IRb which has nothing to do with the above: in IRb, the local variable _ is automatically assigned the value of the last expression that was evaluated in the current IRb session. But this is purely a feature of IRb and has nothing to do with Ruby.

Treatment in linters / style-checkers / static analyzers

The default rulesets in many linters, style-checkers, and static analyzers forbid unused parameters and local variables, on the assumption that this usually indicates a typo, a programming error, or leftover code from a refactoring. Similar to Ruby itself, they usually do allow this for variables and parameters starting with _.

This has nothing to do with the Ruby language itself, though, it is a matter of the Ruby community, more precisely, the developers of those tools following the majority usage within the community.

答案2

得分: -1

在这种情况下,下划线将允许您跳过不需要的元素。

在其他情况下,下划线等于上次的输出。

$ irb
>> 2*3
=> 6
>> _ + 7
=> 13
>> _
=> 13
英文:

In this case, underscore will allow you to skip elements you do not need.

In other cases _ equals last output.

$ irb
>> 2*3
=> 6
>> _ + 7
=> 13
>> _
=> 13

huangapple
  • 本文由 发表于 2023年2月18日 16:55:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492224.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定