Module: NilComparable

Defined in:
lib/mega/nilcomparable.rb

Overview

:title: NilComparable

NilComparable does two things. First it make nil comparable, such that all things (except itself) are greater than it.

Secondly it provides a module called NilComparable to include into any other class to allow it to compare itself to NilClass as the greater of the two.

Usage

TODO

Author(s)

  • Paul Brannan

  • Thomas Sawyer

Class Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mega/nilcomparable.rb', line 44

def self.included( mod )
  mod.class_eval %{
    if method_defined?( :<=> )
      alias_method :compare_without_nil, :<=>
    else
      def compare_without_nil( other )
        raise TypeError, "Cannot compare \#{self.inspect} to \#{other.inspect}"
      end
    end
    def <=>( other )
      return 1 if other.nil?
      compare_without_nil( other )
    end
  }
end