ruby 有人能解释一下吗?

8fsztsew  于 2023-02-18  发布在  Ruby
关注(0)|答案(1)|浏览(184)

我想知道是否有人能给我解释一下破折号保持器做什么的?

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

67up9zun1#

在这种情况下,下划线将允许您跳过不需要的元素。
在其他情况下,_等于最后输出。

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

相关问题