[ create a new paste ] login | about

Link: http://codepad.org/Hf0pmeKV    [ raw code | output | fork ]

Ruby, pasted on Dec 25:
################################################################################
# Sun Mar 25 14:35:00 JST 2007
# Python like GTLT opreator
################################################################################

  class FalseClass
    def < (v); false end
    def > (v); false end
    def <= (v); false end
    def >= (v); false end
    # def =~ (v); false end
  end

  [Fixnum, Bignum, Float].each do
    |klass|
      klass.class_eval do
        alias_method(:pylg_old_lt, :<)
        alias_method(:pylg_old_gt, :>)
        alias_method(:pylg_old_elt, :<=)
        alias_method(:pylg_old_egt, :>=)
        def < (v);  v && pylg_old_lt(v)  ? v : false end
        def > (v);  v && pylg_old_gt(v)  ? v : false end
        def <= (v); v && pylg_old_elt(v) ? v : false end
        def >= (v); v && pylg_old_egt(v) ? v : false end
      end
  end

  # Tests #

  require 'test/unit'

  class TC_Test < Test::Unit::TestCase
    def test_test
      assert_equal false,   1 < 1
      assert_equal false,   1 < 1
      assert_equal 2,       1 < 2
      assert_equal 1,       -1 < 0 < 1
      assert_equal 1,       -1 <= 0 < 1
      assert_equal 1,       -1 <= 0 <= 1
      assert_equal 1,       -1 <= 1 <= 1
      assert_equal false,   -1 <= 1 > 1
      assert_equal 1,       -1 <= 2 > 1
      assert_equal 2,       -1 <= 2 >= 2
      assert_equal false,   3 <= 2 <= 3
      # assert_equal false,   1 < 2 =~ 3
      # assert_equal false,   1 < (2 =~ 3) < 4
      # assert_equal 4,       1 < (2 =~ 2) < 4
      # assert_equal 4,       1 < 2 =~ 2 < 4  # 演算子の優先順位の関係で fail
    end
  end


Output:
1
2
3
4
5
6
Loaded suite t
Started
.
Finished in 0.00173 seconds.

1 tests, 11 assertions, 0 failures, 0 errors


Create a new paste based on this one


Comments: