lib/core/facets/comparable/cmp.rb in facets-2.1.3 vs lib/core/facets/comparable/cmp.rb in facets-2.2.0

- old
+ new

@@ -1,42 +1,23 @@ -# TITLE: -# -# Cmp and Succ/Pred -# -# SUMMARY: -# -# Comparison and Succ/Pred extensions. -# -# AUTHORS: -# -# - Florian Gross -# - Thomas Sawyer -# -# NOTES: -# -# - NOTE Are the module methods better as parameterized mixins? -# -# - TODO Maybe improve the naming or the module methods. - -# class Integer # This is needed to allow Integer to look back to # its Numeric ancestor for the new definition of #succ. remove_method( :succ ) end -# module Comparable # Alternate name for comparison operator #<=>. # # 3.cmp(1) #=> 1 # 3.cmp(3) #=> 0 # 3.cmp(10) #=> -1 # # This fundamental compare method is used to keep # comparison compatible with <tt>#succ</tt>. + # + # CREDIT Peter Vanbroekhoven def cmp(o) self<=>o end end @@ -48,10 +29,12 @@ # # "abc".cmp("abc") #=> 0 # "abcd".cmp("abc") #=> 1 # "abc".cmp("abcd") #=> -1 # "xyz".cmp("abc") #=> 1 + # + # CREDIT Peter Vanbroekhoven def cmp(other) return -1 if length < other.length return 1 if length > other.length self <=> other # alphabetic compare @@ -60,10 +43,12 @@ # Allows #succ to take _n_ step increments. # # "abc".succ #=> "abd" # "abc".succ(4) #=> "abg" # "abc".succ(24) #=> "aca" + # + # CREDIT Trans def succ(n=1) s = self n.times { s = s.next } s @@ -75,98 +60,33 @@ # This is the same as #- but it provides an alternative # for common naming between variant classes. # # 4.distance(3) #=> 1 # + # CREDIT Trans + def distance( other ) self - other end # Allows #succ to take _n_ increments. # # 3.succ(2) #=> 5 # + # CREDIT Trans + def succ(n=nil) n ||= 1 self + n end # Provides #pred as the opposite of #succ. # # 3.pred(2) #=> 1 # + # CREDIT Trans + def pred(n=nil) n ||= 1 self - n end end - - -# _____ _ -# |_ _|__ ___| |_ -# | |/ _ \/ __| __| -# | | __/\__ \ |_ -# |_|\___||___/\__| -# -=begin test - - require 'test/unit' - - class TestComparable < Test::Unit::TestCase - - def test_cmp - assert_equal( -1, 3.cmp(4) ) - assert_equal( 0, 3.cmp(3) ) - assert_equal( 1, 3.cmp(2) ) - end - - end - - class TestStringCompare < Test::Unit::TestCase - - def test_cmp - assert_equal( 0, "abc".cmp("abc") ) - assert_equal( -1, "abc".cmp("abcd") ) - assert_equal( 1, "abcd".cmp("abc") ) - assert_equal( -1, "abc".cmp("bcd") ) - assert_equal( 1, "bcd".cmp("abc") ) - end - - def test_succ - assert_equal( "b", "a".succ ) - assert_equal( "b", "a".succ(1) ) - assert_equal( "c", "a".succ(2) ) - assert_equal( "d", "a".succ(3) ) - end - - end - - class TestNumericCompare < Test::Unit::TestCase - - def test_distance - assert_equal( 4, 10.distance(6) ) - assert_equal( 2, 10.distance(8) ) - assert_equal( -2, 7.distance(9) ) - end - - - def test_pred - assert_equal( 3, 4.pred ) - assert_equal( -3, -2.pred ) - assert_equal( 2, 4.pred(2) ) - assert_equal( -4, -2.pred(2) ) - assert_equal( 6, 4.pred(-2) ) - assert_equal( 0, -2.pred(-2) ) - end - - def test_succ - assert_equal( 5, 4.succ ) - assert_equal( -1, -2.succ ) - assert_equal( 6, 4.succ(2) ) - assert_equal( 0, -2.succ(2) ) - assert_equal( 2, 4.succ(-2) ) - assert_equal( -4, -2.succ(-2) ) - end - - end - -=end