Skip to content

Instantly share code, notes, and snippets.

@esmevane
Created February 11, 2013 15:11
Show Gist options
  • Save esmevane/4754993 to your computer and use it in GitHub Desktop.
Save esmevane/4754993 to your computer and use it in GitHub Desktop.
[ Ruby ] Simple minitest + object-oriented recursion example "With interest"
require 'minitest/autorun'
require 'minitest/pride'
class WithInterest
def initialize amount, interest, years = 1
@amount = amount
@interest = interest
@years = years
end
def calculate
last_year? ? product : next_round_product
end
private
def product
@amount * @interest
end
def next_round_product
product + (next_round * @interest)
end
def next_round
WithInterest.new(@amount, @interest, @years - 1).calculate
end
def last_year?
@years == 1
end
end
describe WithInterest do
it "calculates the interest on one year" do
WithInterest.new(100, 1.06).calculate.must_equal 106
end
it "calculates the interest with multiple years" do
sum = (((100 * 1.06) + 100) * 1.06 + 100) * 1.06
WithInterest.new(100, 1.06, 3).calculate.must_equal sum
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment