Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/range_extd/infinity/infinity.rb
Overview
class Object
Overwrite #<=>() so all its sub-classes can be aware of RangeExtd::Infinity objects (the two constants).
Instance Method Summary collapse
-
#<=>(c) ⇒ Object
Overwrite #<=>().
-
#compare_obj_before_infinity ⇒ Object
No overwriting.
Instance Method Details
#<=>(c) ⇒ Object
Overwrite #<=>(). Then, all its sub-classes can be aware of RangeExtd::Infinity objects (the two constants).
In this definition of #<=>, if self is Comparable (by judging whether it has the method [#<=]), it always returns, unless infinity? and positive? are set accordingly, either -1 or 1, depending which of
RangeExtd::Infinity::(NEGATIVE|POSITIVE)
is compared. If self is not Comparable, the original [#<=>] is called, which should return nil (unless both the object_id agree, eg., nil and nil, in which case 0 is returned).
If you define your own class, which is Comparable, you should define the method “<=>” as follows, as in the standard practice when you redefine a method that exists in a superclass;
451 452 453 454 |
# File 'lib/range_extd/infinity/infinity.rb', line 451 def <=>(c) return (-(c.send(__method__, self) || return)) if RangeExtd::Infinity.infinity? c compare_obj_before_infinity(c) end |
#compare_obj_before_infinity ⇒ Object
No overwriting.
420 |
# File 'lib/range_extd/infinity/infinity.rb', line 420 alias_method :compare_obj_before_infinity, :<=> |