, case , , , , , . ( , , .)
"no monkey patching" . , , . , , .
, . , Duck Typing, . , , float? "N/A", , .
, , , . ( , , ).
, , , , -type class (Deposit), .
, , AP nil 0.0% APR.
class Deposit
def initialize(amount, unit='USD', options={})
@amount = amount.to_f
@unit = unit.to_s
@apr = options.fetch(:apr, 0.0).to_f
end
end
"", case.
class Deposit
def to_s
string = "#{@amount} #{@unit}"
string << ", #{@apr * 100.0}% APR" if @apr > 0.0
string
end
end
d = Deposit.new(656.00, 'rupees', apr: 0.065)
d.to_s
e = Deposit.new(100, 'USD', apr: nil)
e.to_s
f = Deposit.new(100, 'USD')
f.to_s
. case case, , . case , .