在并行作业中使用下划线作为变量[复制]

时间:2022-11-04 13:47:53

This question already has an answer here:

这个问题已经有了答案:

In the following, variable _ (underscore) is an Array, foo == "foo", and bar == "bar".

在下面,变量_(下划线)是一个数组,foo = "foo", bar = "bar"。

_, foo, bar = ["", "foo", "bar"]
_ # => ["", "foo", "bar"]

Can someone explain what the underscore does and where it is useful to use?

有人能解释一下下划线是做什么用的吗?

3 个解决方案

#1


4  

In parallel assignment, we sometimes do two things :

在并行作业中,我们有时做两件事:

  • ignore one element ( taking care of _)
  • 忽略一个元素(处理_)

You can reuse an underscore to represent any element you don’t care about:

您可以重用下划线来表示任何您不关心的元素:

a, _, b, _, c = [1, 2, 3, 4, 5]

a # => 1
b # => 3
c # => 5
  • ignore multiple elements ( taking care of * )
  • 忽略多个元素(注意*)

To ignore multiple elements, use a single asterisk — I’m going to call it a ‘naked splat’ for no better reason than that it sounds a bit amusing:

要忽略多个元素,使用一个星号——我将它称为“裸泳”,理由仅仅是它听起来有点好笑:

a, *, b = [1, 2, 3, 4, 5]

a # => 1
b # => 5

Read this blog post Destructuring assignment in Ruby to know more other related things.

阅读这篇博客文章,了解更多相关的东西。

#2


2  

Underscore is simply a placeholder for the variable assignment you're doing there. In your case it basically means ignore the first value in the array. If we didn't have it, you'd do something like this:

下划线只是您在那里执行的变量赋值的占位符。在你的情况下,它基本上意味着忽略数组中的第一个值。如果我们没有它,你会这样做:

ignored, foo, bar = ["", "foo", "bar"]
=> ["", "foo", "bar"]

then not use ignored for anything. It's nicer to use _

然后不要用“忽略”来做任何事情。用_更好

#3


2  

It's just a variable like any other. You could have used quux or whatever other name you like.

它只是一个变量。你可以用quux或者其他你喜欢的名字。

Except … you would get a warning about an unused local variable named quux, whereas variables whose names start with _ don't get a warning. This is an encoding of a convention to use _ as the name of a variable that you want to ignore.

但是,您会得到一个关于一个未使用的局部变量quux的警告,而名称以_开头的变量则不会得到警告。这是使用_作为您想要忽略的变量名的一种编码。

#1


4  

In parallel assignment, we sometimes do two things :

在并行作业中,我们有时做两件事:

  • ignore one element ( taking care of _)
  • 忽略一个元素(处理_)

You can reuse an underscore to represent any element you don’t care about:

您可以重用下划线来表示任何您不关心的元素:

a, _, b, _, c = [1, 2, 3, 4, 5]

a # => 1
b # => 3
c # => 5
  • ignore multiple elements ( taking care of * )
  • 忽略多个元素(注意*)

To ignore multiple elements, use a single asterisk — I’m going to call it a ‘naked splat’ for no better reason than that it sounds a bit amusing:

要忽略多个元素,使用一个星号——我将它称为“裸泳”,理由仅仅是它听起来有点好笑:

a, *, b = [1, 2, 3, 4, 5]

a # => 1
b # => 5

Read this blog post Destructuring assignment in Ruby to know more other related things.

阅读这篇博客文章,了解更多相关的东西。

#2


2  

Underscore is simply a placeholder for the variable assignment you're doing there. In your case it basically means ignore the first value in the array. If we didn't have it, you'd do something like this:

下划线只是您在那里执行的变量赋值的占位符。在你的情况下,它基本上意味着忽略数组中的第一个值。如果我们没有它,你会这样做:

ignored, foo, bar = ["", "foo", "bar"]
=> ["", "foo", "bar"]

then not use ignored for anything. It's nicer to use _

然后不要用“忽略”来做任何事情。用_更好

#3


2  

It's just a variable like any other. You could have used quux or whatever other name you like.

它只是一个变量。你可以用quux或者其他你喜欢的名字。

Except … you would get a warning about an unused local variable named quux, whereas variables whose names start with _ don't get a warning. This is an encoding of a convention to use _ as the name of a variable that you want to ignore.

但是,您会得到一个关于一个未使用的局部变量quux的警告,而名称以_开头的变量则不会得到警告。这是使用_作为您想要忽略的变量名的一种编码。