`&:` 在此 Ruby 片段中是什么意思?
What does `&:` mean in this Ruby snippet?
这是我在竞争性编程网站上看到的解决方案:
gets.split.map &:to_i
&:to_i
短语似乎等同于 {|s| s.to_i}
,但我在 ruby-doc.org 找不到关于该语法的任何信息或匹配项"&:" [ruby] 在这里。 (我也不知道英文搜索应该怎么称呼它。)
如果重要,该站点正在使用 Ruby 3.0.1.
这是 Symbol#to_proc
的示例 - 请参阅 https://www.brianstorti.com/understanding-ruby-idiom-map-with-symbol/ for a more detailed explanation, or the ruby API documentation
这是我在竞争性编程网站上看到的解决方案:
gets.split.map &:to_i
&:to_i
短语似乎等同于 {|s| s.to_i}
,但我在 ruby-doc.org 找不到关于该语法的任何信息或匹配项"&:" [ruby] 在这里。 (我也不知道英文搜索应该怎么称呼它。)
如果重要,该站点正在使用 Ruby 3.0.1.
这是 Symbol#to_proc
的示例 - 请参阅 https://www.brianstorti.com/understanding-ruby-idiom-map-with-symbol/ for a more detailed explanation, or the ruby API documentation