Ruby:将self方法应用于每个参数

时间:2016-10-14 13:30:39

标签: ruby enumerable

Ruby中是否有更好的方法可以使用可枚举的每个参数调用self方法?会减少以下代码的东西:

class Cook
  def cook_dinner(*recipes)
    recipes.each { |recipe| cook recipe }
  end

  def cook(recipe)
    do_something_with recipe
  end
end

这样的事情?

class Cook
  def cook_dinner(*recipes)
    recipes.apply_to_each(:cook)
  end

  def cook(recipe)
    do_something_with recipe
  end
end

0 个答案:

没有答案