【问题标题】:ruby passing &:key as an argument to map instead of a blockruby 将 &:key 作为参数传递给 map 而不是块
【发布时间】:2017-05-16 13:41:55
【问题描述】:

我写了以下代码:

def report_view_classes
  [
    Hourdiff,
    AverageTimeForFirstReferrals,
    CountZipCodeByUserids,
    FirstRentForUsers,
  ]
end

def load_views
  report_view_classes.each do |report_view_class|
    report_view_class.create
  end

但是 rubocop 给了我以下警告:

C: Pass &:create as an argument to each instead of a block.
  report_view_classes.each do |report_view_class

我很困惑该怎么办

【问题讨论】:

标签: ruby rubocop


【解决方案1】:

Ruby 有一个简单块的简写。

["apple", "pear", "grape"].map { |fruit| fruit.upcase }

# This does the same thing with less code:

["apple", "pear", "grape"].map(&:upcase)

【讨论】:

    猜你喜欢
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    相关资源
    最近更新 更多