【发布时间】:2021-06-04 00:13:00
【问题描述】:
我有以下构造(简化):
puts ['a!', 'b!', 'c?'].select { |f| f if f.end_with?('!') }
打印出来
a!
b!
我想用一个 & 来使它更短:
puts ['a!', 'b!', 'c?'].select(&:end_with?('!'))
但我得到一个错误:
..., 'c?'].select(&:end_with?('!'))
_
x.rb:4: syntax error, unexpected ')', expecting end-of-input
我该如何解决这个问题?
【问题讨论】:
-
['a!', 'b!', 'c?'].grep(/!\z/);-)