{|_, e|e.length>1} underScore ( _ ) 在 Ruby 中有什么用? [英] {|_, e| e.length>1} what is the use of underScore ( _ ) in Ruby?

查看:107
本文介绍了{|_, e|e.length>1} underScore ( _ ) 在 Ruby 中有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

country =
  ["UK", "US", "RS", "EU", "UK", "US"].
    group_by{ |e| e }.
    keep_if{ |_, e | e.length > 1 }
#⇒ {"UK"=>["UK", "UK"], "US"=>["US", "US"]}

第二块中_(下划线)的作用是什么.有人能详细解释一下吗?

What is the use of _ (underscore) in the second block. Can someone explain in details?

推荐答案

对此有两个层面的三个答案.

There are three answers to this on two levels.

第一个答案是在 Ruby 语言层面:Ruby 中没有使用下划线.它没有任何意义.它只是一个合法标识符,就像任何其他合法标识符一样.它可以被命名为 foobar,并且它不会以任何方式改变这段代码的含义.

The first answer is on the level of the Ruby language: there is no use of the underscore in Ruby. It has no meaning. It is just a legal identifier like any other legal identifier. It could have been named foo or bar instead, and it would not in any way change the meaning of this code.

第二个答案是在 Ruby 社区的层面上:在 Ruby 社区中,下划线用于与其他阅读代码的程序员交流我需要在这里放置一个标识符,因为 Ruby 语言的规则迫使我,但我实际上对它并不感兴趣,我实际上永远不会使用它."所以,它对 Ruby 没有意义,但它确实对 Ruby 程序员有意义.就像以 !? 结尾的方法一样:它们没有特殊含义,它们只是用来将一个 Ruby 程序员的意图传达给另一个程序员.

The second answer is on the level of the Ruby community: in the Ruby community, the underscore is used to communicate to other programmers reading the code "I need to put an identifier here because the rules of the Ruby language force me to, but I am not actually interested in it, I will in fact never use it." So, it has no meaning to Ruby, but it does have meaning to Ruby programmers. It's just like methods ending in ! or ?: they have no special meaning, they are just used to communicate intent from one Ruby programmer to another.

第三个答案再次出现在 Ruby 语言级别:由于 #2,在 Ruby 1.9+ 中进行了两项更改,将下划线的使用编码为忽略我"标识符:

The third answer is again on the level of the Ruby language: because of #2, there were two changes made in Ruby 1.9+ that codify the uses of the underscore as an "ignore me" identifier:

  1. 通常,未使用的局部变量会生成警告.未使用的以下划线开头或简单命名为 _ 的局部变量不会生成警告.
  2. 通常,一个标识符在一个参数列表中只能使用一次.以下划线开头或简单命名为 _ 的标识符可以多次使用.
  1. Normally, unused local variables generate warnings. Unused local variables that start with an underscore, or are simply named _ do not generate warnings.
  2. Normally, an identifier can only be used once in a parameter list. Identifiers that start with an underscore, or are simply named _ can be used multiple times.

见:

#!/usr/bin/ruby -w

foo = nil
bar = nil
baz = nil

运行:

./test.rb
./test.rb:3: warning: assigned but unused variable - foo
./test.rb:4: warning: assigned but unused variable - bar
./test.rb:5: warning: assigned but unused variable - baz

但是:

#!/usr/bin/ruby -w

_foo = nil
_bar = nil
_    = nil

运行:

./test.rb

还有:

#!/usr/bin/ruby -w

def foo(bar, bar) end

运行:

./test.rb
./test.rb:3: duplicated argument name
def foo(bar, bar); end

[第二个bar在控制台有下划线,这里很难重现.]

[The second bar is underlined in the console, which is hard to reproduce here.]

但是:

#!/usr/bin/ruby -w

def foo(_bar, _bar) end

运行:

./test.rb

因此,有下划线和无下划线的唯一区别是下划线会关闭某些错误和警告,这与其在 Ruby 社区中的常见用法一致.

So, the only difference between an underscore and no underscore is that the underscore turns off certain errors and warnings, in line with its common usage within the Ruby community.

这个约定通常也被 linter、静态分析器、编辑器和提供 Ruby 代码语义分析的 IDE 所遵守,例如对于名称以 _ 开头或以 _ 开头的变量,突出显示和警告未使用变量的 IDE 不会这样做.

This convention is generally also respected by linters, static analyzers, editors, and IDEs providing semantic analysis of Ruby code, e.g. IDEs that highlight and warn about unused variables will not do so for variables whose name begins with or is _.

这篇关于{|_, e|e.length>1} underScore ( _ ) 在 Ruby 中有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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