Skip to content

Instantly share code, notes, and snippets.

@Manfred
Forked from alloy/gist:942157
Created April 26, 2011 12:31
Show Gist options
  • Save Manfred/942173 to your computer and use it in GitHub Desktop.
Save Manfred/942173 to your computer and use it in GitHub Desktop.
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
Copy link

alloy commented Apr 26, 2011

And RSpec/Bacon agnostic!

@lrz
Copy link

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