RuboCop 风格建议:"Pass '$:to_i' as an argument to 'Transform' instead of a block."

RuboCop style suggestion: "Pass '$:to_i' as an argument to 'Transform' instead of a block."

我想我不太理解样式建议。我正在将正则表达式传递给 Transform,这是否被视为 "block"?在这种情况下如何将 $:key 传递给 Transform?

CAPTURE_CASH_AMOUNT = Transform(/^$(\d+)$/) do |digits|
  digits.to_i
end

[...] is this considered a "block"?

do-end 中的任何内容都是 Ruby 中的一个块。

And how do I pass &:key to Transform in this situation?

Ruby 为您实现 Symbol#to_proc,允许 shorthand &:method 块的参数将单个方法发送到产生的对象。

在你的例子中,这等同于:

CAPTURE_CASH_AMOUNT = Transform(/^$(\d+)$/, &:to_i)