File: rational.rb

package info (click to toggle)
mruby 3.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,584 kB
  • sloc: ansic: 51,933; ruby: 29,510; yacc: 7,077; cpp: 517; makefile: 51; sh: 42
file content (22 lines) | stat: -rw-r--r-- 291 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Rational < Numeric
  def inspect
    "(#{to_s})"
  end

  def to_s
    "#{numerator}/#{denominator}"
  end

  def <=>(other)
    return nil unless other.kind_of?(Numeric)
    self.to_f <=> other.to_f
  rescue
    nil
  end
end

class Numeric
  def to_r
    Rational(self, 1)
  end
end