Class: Numeric

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

Overview

class Numeric

Modify #> and #< 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.

To activate these features, explicitly do one of the following

require "range_extd/numeric"
require "range_extd/object"
require "range_extd/load_all"

Instance Method Summary collapse

Instance Method Details

#<(c) ⇒ Object

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



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/range_extd/numeric.rb', line 52

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.



26
27
28
29
30
31
32
# File 'lib/range_extd/numeric.rb', line 26

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.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/range_extd/numeric.rb', line 37

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

Backup of the original #<=>



24
# File 'lib/range_extd/numeric.rb', line 24

alias_method :compare_than_numeric_before_infinity?, :<=>

#greater_than_numeric_before_infinity?Object

Backup of the original #>



35
# File 'lib/range_extd/numeric.rb', line 35

alias_method :greater_than_numeric_before_infinity?, :>

#less_than_numeric_before_infinity?Object

Backup of the original #<



50
# File 'lib/range_extd/numeric.rb', line 50

alias_method :less_than_numeric_before_infinity?, :<