If I decide to use Rationals in Ruby for a console application, but don't want them displayed as a fraction, is there an idiomatic way to specify they should be displayed in decimal notation?
Options I know about include:
fraction = Rational(1, 2)
puts "Use to_f: #{fraction.to_f}"
puts(sprintf("Use sprintf %f", fraction))
class Rational
def to_s
to_f.to_s
end
end
puts "Monkey patch Rational#to_s: #{fraction}"
Are there any alternatives?