Hi all,
I want to make strings like this:
$100.00 plus $22.00 per month
$100.00
$22.00 per week
free (I want this when both numbers are zero)
Generally: [one off fee] plus [fee per billing cycle] per [billing cycle length]
Based on this input:
per_license_cost (= one off fee)
per_cycle_cost (= fee per cycle)
currency_symbol (=$)
cycle_length (= a string: 'day' | 'week' | 'month' | 'year')
I have working code, and in any other language I'd take the uglyness
for granted, but in Ruby I still have hope. So, what would be a better
way to write this? I've put the code I put together at the bottom.
Thanks,
Douglas
The code:
def human_price
if per_license_cost.zero? and per_cycle_cost.zero?
'free'
elsif per_cycle_cost.zero?
human_per_license_cost
elsif per_license_cost.zero?
human_per_cycle_cost
else
human_per_license_cost + ' plus ' + human_per_cycle_cost
end
end
def currency_symbol
'$' # fine for now... should I think about a Money class to manage this?
end
private
def human_cost(number)
human = number.to_s
number = if number.abs.to_s.length > 2
human[-2, 0] = '.'
human
elsif number.abs.to_s.length == 2
'0.' + human
elsif number.abs.to_s.length == 1
'0.0' + human
else
'0.00'
end
return currency_symbol + number
end
def human_per_license_cost
human_cost(per_license_cost)
end
def human_per_cycle_cost
human_cost(per_cycle_cost) + ' per ' + cycle_length
end