Guest User

anekos

a guest
Dec 25th, 2008
130
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ################################################################################
  2. # Sun Mar 25 14:35:00 JST 2007
  3. # Python like GTLT opreator
  4. ################################################################################
  5.  
  6.   class FalseClass
  7.     def < (v); false end
  8.     def > (v); false end
  9.     def <= (v); false end
  10.     def >= (v); false end
  11.     # def =~ (v); false end
  12.   end
  13.  
  14.   [Fixnum, Bignum, Float].each do
  15.     |klass|
  16.       klass.class_eval do
  17.         alias_method(:pylg_old_lt, :<)
  18.         alias_method(:pylg_old_gt, :>)
  19.         alias_method(:pylg_old_elt, :<=)
  20.         alias_method(:pylg_old_egt, :>=)
  21.         def < (v);  v && pylg_old_lt(v)  ? v : false end
  22.         def > (v);  v && pylg_old_gt(v)  ? v : false end
  23.         def <= (v); v && pylg_old_elt(v) ? v : false end
  24.         def >= (v); v && pylg_old_egt(v) ? v : false end
  25.       end
  26.   end
  27.  
  28.   # Tests #
  29.  
  30.   require 'test/unit'
  31.  
  32.   class TC_Test < Test::Unit::TestCase
  33.     def test_test
  34.       assert_equal false,   1 < 1
  35.       assert_equal false,   1 < 1
  36.       assert_equal 2,       1 < 2
  37.       assert_equal 1,       -1 < 0 < 1
  38.       assert_equal 1,       -1 <= 0 < 1
  39.       assert_equal 1,       -1 <= 0 <= 1
  40.       assert_equal 1,       -1 <= 1 <= 1
  41.       assert_equal false,   -1 <= 1 > 1
  42.       assert_equal 1,       -1 <= 2 > 1
  43.       assert_equal 2,       -1 <= 2 >= 2
  44.       assert_equal false,   3 <= 2 <= 3
  45.       # assert_equal false,   1 < 2 =~ 3
  46.       # assert_equal false,   1 < (2 =~ 3) < 4
  47.       # assert_equal 4,       1 < (2 =~ 2) < 4
  48.       # assert_equal 4,       1 < 2 =~ 2 < 4  # &#28436;&#31639;&#23376;&#12398;&#20778;&#20808;&#38918;&#20301;&#12398;&#38306;&#20418;&#12391; fail
  49.     end
  50.   end
RAW Paste Data