Class: Integer

Inherits:
Numeric show all
Defined in:
lib/builtinME.rb,
lib/rationalME.rb

Direct Known Subclasses

Fixnum

Instance Method Summary collapse

Methods inherited from Numeric

#abs, #div, #divmod, #floor, #modulo, #nonzero?, #remainder, #zero?

Methods included from Comparable

#<, #<=, #==, #>, #>=, #between?

Instance Method Details

#denominatorObject

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



417
418
419
# File 'lib/rationalME.rb', line 417

def denominator
  1
end

#downto(to) ⇒ Object



455
456
457
458
459
460
461
# File 'lib/builtinME.rb', line 455

def downto(to)
    a = self
    while a >= to
        yield a
        a -= 1
    end
end

#gcd(other) ⇒ Object

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.



438
439
440
441
442
443
444
445
446
447
# File 'lib/rationalME.rb', line 438

def gcd(other)
  min = self.abs
  max = other.abs
  while min > 0
    tmp = min
    min = max % min
    max = tmp
  end
  max
end

#gcdlcm(other) ⇒ Object

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]


473
474
475
476
477
478
479
480
# File 'lib/rationalME.rb', line 473

def gcdlcm(other)
  gcd = self.gcd(other)
  if self.zero? or other.zero?
    [gcd, 0]
  else
    [gcd, (self.div(gcd) * other).abs]
  end
end

#integer?Boolean

Always returns true

Returns:

  • (Boolean)


443
444
445
# File 'lib/builtinME.rb', line 443

def integer?
    true
end

#lcm(other) ⇒ Object

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

Examples:

6.lcm 7        # -> 42
6.lcm 9        # -> 18


457
458
459
460
461
462
463
# File 'lib/rationalME.rb', line 457

def lcm(other)
  if self.zero? or other.zero?
    0
  else
    (self.div(self.gcd(other)) * other).abs
  end
end

#nextObject

Returns the Integer equal to int + 1



433
434
435
# File 'lib/builtinME.rb', line 433

def next
    self + 1
end

#numeratorObject

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



410
411
412
# File 'lib/rationalME.rb', line 410

def numerator
  self
end

#sizeObject



463
464
465
# File 'lib/builtinME.rb', line 463

def size
    4
end

#succObject

Synonym for Integer#next



438
439
440
# File 'lib/builtinME.rb', line 438

def succ
    self + 1
end

#to_iObject Also known as: to_int



426
427
428
# File 'lib/builtinME.rb', line 426

def to_i
    return self
end

#to_rObject

Returns a Rational representation of this integer.



424
425
426
# File 'lib/rationalME.rb', line 424

def to_r
  Rational(self, 1)
end

#upto(to) ⇒ Object



447
448
449
450
451
452
453
# File 'lib/builtinME.rb', line 447

def upto(to)
    a = self
    while a <= to
        yield a
        a += 1
    end
end