In Files

  • mrbgems/mruby-rational/mrblib/rational.rb

Parent

Rational

Public Instance Methods

*(rhs) click to toggle source
 
               # File mrbgems/mruby-rational/mrblib/rational.rb, line 10
def *(rhs)
  if rhs.is_a? Rational
    Rational(numerator * rhs.numerator, denominator * rhs.denominator)
  elsif rhs.is_a? Integer
    Rational(numerator * rhs, denominator)
  elsif rhs.is_a? Numeric
    numerator * rhs / denominator
  end
end
            
+(rhs) click to toggle source
 
               # File mrbgems/mruby-rational/mrblib/rational.rb, line 20
def +(rhs)
  if rhs.is_a? Rational
    Rational(numerator * rhs.denominator + rhs.numerator * denominator, denominator * rhs.denominator)
  elsif rhs.is_a? Integer
    Rational(numerator + rhs * denominator, denominator)
  elsif rhs.is_a? Numeric
    (numerator + rhs * denominator) / denominator
  end
end
            
-(rhs) click to toggle source
 
               # File mrbgems/mruby-rational/mrblib/rational.rb, line 30
def -(rhs)
  if rhs.is_a? Rational
    Rational(numerator * rhs.denominator - rhs.numerator * denominator, denominator * rhs.denominator)
  elsif rhs.is_a? Integer
    Rational(numerator - rhs * denominator, denominator)
  elsif rhs.is_a? Numeric
    (numerator - rhs * denominator) / denominator
  end
end
            
/(rhs) click to toggle source
 
               # File mrbgems/mruby-rational/mrblib/rational.rb, line 40
def /(rhs)
  if rhs.is_a? Rational
    Rational(numerator * rhs.denominator, denominator * rhs.numerator)
  elsif rhs.is_a? Integer
    Rational(numerator, denominator * rhs)
  elsif rhs.is_a? Numeric
    numerator / rhs / denominator
  end
end
            
<=>(rhs) click to toggle source
 
               # File mrbgems/mruby-rational/mrblib/rational.rb, line 50
def <=>(rhs)
  if rhs.is_a?(Integral)
    return numerator <=> rhs if denominator == 1
    rhs = Rational(rhs)
  end

  case rhs
  when Rational
    (numerator * rhs.denominator - denominator * rhs.numerator) <=> 0
  when Numeric
    (rhs <=> self)&.-@
  else
    nil
  end
end
            
==(rhs) click to toggle source
 
               # File mrbgems/mruby-rational/mrblib/rational.rb, line 66
def ==(rhs)
  return true if self.equal?(rhs)
  if rhs.is_a?(Integral) && denominator == 1
    return numerator == rhs
  end
  if rhs.is_a?(Rational)
    numerator * rhs.denominator == denominator * rhs.numerator
  else
    rhs == self
  end
end
            
inspect() click to toggle source
 
               # File mrbgems/mruby-rational/mrblib/rational.rb, line 2
def inspect
  "(#{to_s})"
end
            
to_s() click to toggle source
 
               # File mrbgems/mruby-rational/mrblib/rational.rb, line 6
def to_s
  "#{numerator}/#{denominator}"
end