Saturday, 13 December 2014

12 Tips of Christmas - 3 Wrap instantiation inside a class method

3. Wrap instantiation inside a class method

Quite often I find myself when creating report objects, wanting to do the following:
@report = Report.new(params)
@report = @report.report()
Why not cut to the chase, by creating a report class method, that returns your shiny instantiated report for you:
class Report

  def self.report(params)
    report = self.new(params).report()
  end
end
Now we can simply call the following, whenever we want a new instance of our report:
@report = Report.report(params)
Much easier.

No comments:

Post a Comment