Skip to content

Instantly share code, notes, and snippets.

@JoelBeasley
Last active August 29, 2015 14:13
Show Gist options
  • Save JoelBeasley/fbc437dc3e00c520b8b1 to your computer and use it in GitHub Desktop.
Save JoelBeasley/fbc437dc3e00c520b8b1 to your computer and use it in GitHub Desktop.
Refactoring Method after watching a Ben Orenstein OO talk.
def calc_earnings_change_history(number_of_years)
earnings_history = Array.new
number_of_months(number_of_years).each_with_index do |month_as_integer, index|
this_month = month_as_integer.months.ago.end_of_month
next_month = this_month.next_month
last_month = this_month.prev_month
percent_change = index == 0 ? 0.0 : percent_change_between_months(share_price_on(last_month), share_price_on(this_month))
earnings_history << [chart_time_for(this_month), percent_change]
end
earnings_history
end
def chart_time_for(time)
time.to_i * 1000
end
def percent_change_between_months(last_month, this_month)
last_month.percent_change(this_month).to_f / 100
end
def number_of_months(number_of_years)
months = number_of_years * 12
0..months
end
def calc_earnings_change_history(number_of_years)
response = Array.new
months = number_of_years * 12
number_of_months = 0..months
number_of_months.to_a.reverse.each_with_index do |month_offset, index|
date = month_offset.months.ago.end_of_month
this_month = date
next_month = date.next_month
this_month_share_price = share_price_on(this_month).to_f
if index == 0
percent_change = 0.0
else
last_month = this_month.prev_month
last_month_share_price = share_price_on(last_month).to_f
percent_change = last_month_share_price.percent_change(this_month_share_price) / 100
end
response << [this_month.to_i * 1000, percent_change]
end
response
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment