Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Square job interview exercise
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!
@alloy

This comment has been minimized.

Copy link

@alloy alloy commented Apr 26, 2011

And RSpec/Bacon agnostic!

@lrz

This comment has been minimized.

Copy link

@lrz lrz commented Apr 26, 2011

Okay that one is elite :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment