Skip to content

Instantly share code, notes, and snippets.

@alloy
Forked from mattetti/gist:189283
Created September 19, 2009 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alloy/189360 to your computer and use it in GitHub Desktop.
Save alloy/189360 to your computer and use it in GitHub Desktop.
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