无法看懂是如何避免此种操作的,求解...
class Duration
def inspect
value = @proxy
parts = %w[days hours minutes seconds].inject([]) do |units, unit|
if value >= 1.send(unit)
quotient, value = value.divmod 1.send(unit)
units + [[quotient, unit]]
else
units
end
end
parts.map! {|value, unit| "#{value} #{value == 1 ? unit.chop : unit}" }
parts.join(', ')
end
end