Class: Numeric

Inherits:
Object show all
Defined in:
lib/range_extd/infinity/infinity.rb

Overview

class Numeric

Modify #> and #< because 5 < RangeExtd::Infinity::POSITIVE raises ArgumentError(!). In other words, Integer#< does not respect Object#<=> but rewrites it.

I do not know if it has been always the case, or some changes have been made in more recent versions of Ruby.

Note that Float#< etc need to be redefined individually, because they seem not to use Numeric#< any more.

Instance Method Summary collapse

Instance Method Details

#<(c) ⇒ Object

Special case for comparison with a RangeExtd::Infinity instance.



498
499
500
501
502
503
504
505
506
507
508
# File 'lib/range_extd/infinity/infinity.rb', line 498

def <(c)
  # Default if self is Complex or something not Integer, Rational, Float or alike
  # or the special case INFINITY.
  return less_than_numeric_before_infinity?(c) if !self.class.method_defined?(:>) || ((abs rescue self) == Float::INFINITY)

  if RangeExtd::Infinity.infinity? c
    c.positive?
  else
    less_than_numeric_before_infinity?(c)
  end
end

#<=>(c) ⇒ Object

Special case for comparison with a RangeExtd::Infinity instance.



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

def <=>(c)
  # Default if the special case INFINITY.
  return compare_than_numeric_before_infinity?(c) if ((abs rescue self) == Float::INFINITY)

  return (-(c.send(__method__, self) || return)) if RangeExtd::Infinity.infinity? c
  compare_than_numeric_before_infinity?(c)
end

#>(c) ⇒ Object

Special case for comparison with a RangeExtd::Infinity instance.



484
485
486
487
488
489
490
491
492
493
494
# File 'lib/range_extd/infinity/infinity.rb', line 484

def >(c)
  # Default if self is Complex or something not Integer, Rational, Float or alike
  # or the special case INFINITY.
  return greater_than_numeric_before_infinity?(c) if !self.class.method_defined?(:>) || ((abs rescue self) == Float::INFINITY)

  if RangeExtd::Infinity.infinity? c
    c.negative?
  else
    greater_than_numeric_before_infinity?(c)
  end
end

#compare_than_numeric_before_infinity?Object

No overwriting.



472
# File 'lib/range_extd/infinity/infinity.rb', line 472

alias_method :compare_than_numeric_before_infinity?, :<=>

#greater_than_numeric_before_infinity?Object

No overwriting.



482
# File 'lib/range_extd/infinity/infinity.rb', line 482

alias_method :greater_than_numeric_before_infinity?, :>

#less_than_numeric_before_infinity?Object

No overwriting.



496
# File 'lib/range_extd/infinity/infinity.rb', line 496

alias_method :less_than_numeric_before_infinity?, :<