-
-
Save alloy/189360 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'active_support' | |
def rates | |
o = Object.new | |
def o.summer_months_nbr | |
[6, 7, 8, 9] | |
end | |
o | |
end | |
# monthly_usage should not be zero based based, but rather 1..12 like actual months. | |
# | |
# Returns a simple demo hash with usage per month. Eg. january: { 1 => 1 } | |
# | |
# You could also unshift an extra object onto an array so the month numbers line up. YMMV | |
def monthly_usage | |
Hash[*(1..12).zip(1..12).flatten] | |
end | |
# Rails | |
def summer_monthly_usage | |
@summer_monthly_usage ||= monthly_usage.values_at(*rates.summer_months_nbr).sum | |
end | |
p summer_monthly_usage # => 30 | |
# 1.9 | |
def summer_monthly_usage | |
@summer_monthly_usage ||= monthly_usage.values_at(*rates.summer_months_nbr).inject(&:+) | |
end | |
p summer_monthly_usage # => 30 | |
# 1.8 | |
def summer_monthly_usage | |
@summer_monthly_usage ||= monthly_usage.values_at(*rates.summer_months_nbr).inject { |sum, u| sum + u } | |
end | |
p summer_monthly_usage # => 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment