Extended maintenance of Ruby versions 1.8.7 and 1.9.2 will end on July 31, 2014. Read more

In Files

  • rational.rb

Class/Module Index [+]

Quicksearch

Integer

Public Instance Methods

denominator() click to toggle source

In an integer, the denominator is 1. Therefore, this method returns 1.

 
               # File rational.rb, line 449
def denominator
  1
end
            
gcd(other) click to toggle source

Returns the greatest common denominator of the two numbers (self and n).

Examples:

72.gcd 168           # -> 24
19.gcd 36            # -> 1

The result is positive, no matter the sign of the arguments.

 
               # File rational.rb, line 470
def gcd(other)
  min = self.abs
  max = other.abs
  while min > 0
    tmp = min
    min = max % min
    max = tmp
  end
  max
end
            
gcdlcm(other) click to toggle source

Returns the GCD and the LCM (see gcd and lcm) of the two arguments (self and other). This is more efficient than calculating them separately.

Example:

6.gcdlcm 9     # -> [3, 18]
 
               # File rational.rb, line 505
def gcdlcm(other)
  gcd = self.gcd(other)
  if self.zero? or other.zero?
    [gcd, 0]
  else
    [gcd, (self.div(gcd) * other).abs]
  end
end
            
lcm(other) click to toggle source

Returns the lowest common multiple (LCM) of the two arguments (self and other).

Examples:

6.lcm 7        # -> 42
6.lcm 9        # -> 18
 
               # File rational.rb, line 489
def lcm(other)
  if self.zero? or other.zero?
    0
  else
    (self.div(self.gcd(other)) * other).abs
  end
end
            
numerator() click to toggle source

In an integer, the value is the numerator of its rational equivalent. Therefore, this method returns self.

 
               # File rational.rb, line 442
def numerator
  self
end
            
to_r() click to toggle source

Returns a Rational representation of this integer.

 
               # File rational.rb, line 456
def to_r
  Rational(self, 1)
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus