Skip to content

Instantly share code, notes, and snippets.

@scottswezey
Created July 27, 2010 01:14
Show Gist options
  • Save scottswezey/491555 to your computer and use it in GitHub Desktop.
Save scottswezey/491555 to your computer and use it in GitHub Desktop.
# **Updates**
%w{body_labor refinish_labor frame_labor}.each do |rate|
%w{mean mathematical_mode lowest highest}.each do |method|
@result.send(rate).send(method)
end
end
# **Original**
%table
%tr
%th
%th Avg.
%th Mode
%th Min.
%th Max.
%tr
%th Body Labor
%td= pretty_number_to_currency @result.body_labor.mean
%td= pretty_number_to_currency @result.body_labor.mathematical_mode
%td= pretty_number_to_currency @result.body_labor.lowest
%td= pretty_number_to_currency @result.body_labor.highest
%tr
%th Refinish Labor
%td= pretty_number_to_currency @result.refinish_labor.mean
%td= pretty_number_to_currency @result.refinish_labor.mathematical_mode
%td= pretty_number_to_currency @result.refinish_labor.lowest
%td= pretty_number_to_currency @result.refinish_labor.highest
@scottswezey
Copy link
Author

So I have this table in one of my haml views. The first header row is its own thing, but after that I have maybe 10 more rows that are almost an exact match to the first two content rows. Instead of @result.refinish_labor, its @result.something_else each time... Can someone give me some tips on how to best refactor this?

@sirupsen
Copy link

Do something like:

  • %w{mean mathematica lowest highest}.each do |method|
    = pretty_number_to_currency @result.refinish_labor.send(method)
  • end

For maximum awesome, make it a helper accepting an Array to keep it DRY .

@scottswezey
Copy link
Author

Can that also be applied to the middle portion?

@result.SOMETHING.send(method)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment