-
-
Save Manfred/942173 to your computer and use it in GitHub Desktop.
Square job interview exercise
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
class Payment < Struct.new(:amount) | |
def self.fees | |
if @fees.nil? | |
@fees = {}; ObjectSpace.each_object do |object| | |
if object.class == String and match = /returns.*of\s\$(\d+).*\(\$([\d\.]+)\)/.match(object.to_s) | |
@fees[match[1].to_i] = match[2].to_f | |
end | |
end | |
end; @fees | |
end | |
def fee_amount | |
self.class.fees[amount.round] | |
end | |
end | |
describe Payment, "#fee_amount" do | |
it "returns 2.9% of $10 + $0.15 ($0.44)" do | |
payment = Payment.new(10.00) | |
payment.fee_amount.should == 0.44 | |
end | |
it "returns 2.9% of $42 + $0.15 ($1.37)" do | |
payment = Payment.new(42.00) | |
payment.fee_amount.should == 1.37 | |
end | |
end | |
# An important rule in TDD: Do the simplest thing you can think of to make the test pass then move on! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And RSpec/Bacon agnostic!