Comparable

Comparable

ri

 ------------------------------------------------------ Class: Comparable
      The  Comparable  mixin is used by classes whose objects may be
      ordered. The class must define the  <=>  operator, which compares
      the receiver against another object, returning -1, 0, or  1
      depending on whether the receiver is less than, equal to, or
      greater than the other object.  Comparable  uses  <=>  to implement
      the conventional comparison operators ( < ,  <= ,  == ,  >= , and
       > ) and the method  between? .
 
         class SizeMatters
           include Comparable
           attr :str
           def <=>(anOther)
             str.size <=> anOther.str.size
           end
           def initialize(str)
             @str = str
           end
           def inspect
             @str
           end
         end
      
         s1 = SizeMatters.new("Z")
         s2 = SizeMatters.new("YY")
         s3 = SizeMatters.new("XXX")
         s4 = SizeMatters.new("WWWW")
         s5 = SizeMatters.new("VVVVV")
      
         s1 < s2                       #=> true
         s4.between?(s1, s3)           #=> false
         s4.between?(s3, s5)           #=> true
         [ s3, s2, s5, s4, s1 ].sort   #=> [Z, YY, XXX, WWWW, VVVVV]
 
 ------------------------------------------------------------------------
 
 
 Instance methods:
 -----------------
      <, <=, ==, >, >=, between?

refe

 ==== Comparable ====
 比較演算を許すクラスのための Mix-in。このモジュールをインクルー
 ドするクラスは、基本的な比較演算子である <=> 演算子を定義してい
 る必要があります。他の比較演算子はその定義を利用して派生できます。
 ---- Singleton methods ----
 ---- Instance methods ----
 < <= == > >= between?
 ---- Singleton methods (inherited) ----
 ---- Instance methods (inherited) ----

  • 2006/05/12 19:52:48 <
  • 2006/05/12 19:53:03 <=
  • 2006/05/12 19:53:19 ==
  • 2006/05/12 19:53:41 >
  • 2006/05/12 19:53:58 >=
  • 2006/05/15 12:02:14 ancestors
  • 2006/05/15 12:04:32 between?
  • 2006/05/12 19:56:08 Comparable
  • 2006/05/12 20:13:53 Numeric
  • 2006/05/12 20:37:34 Range
  • 2006/05/15 11:49:03 String
  • 2006/05/15 11:55:30 Time
  • 2006/05/15 11:59:52 ^