I find out that bitwise operation doesn't call coerce.
How to make mimic Integer class?


###

class IntegerMimic
  def initialize n
    @n = n
  end

  def to_int
    p :to_int
    @n
  end

  def coerce obj
    p :coerce
    [obj, @n]
  end
end

obj = IntegerMimic.new(10)

%w(+ - * / % & | ^).each{|sym|
  p sym
  begin
    p(3.send(sym, obj))
  rescue => e
    p e
  end
}

__END__
#=>
ruby 1.9.4dev (2011-09-11 trunk 33248) [i386-mswin32_100]
"+"
:coerce
13
"-"
:coerce
-7
"*"
:coerce
30
"/"
:coerce
0
"%"
:coerce
3
"&"
#<TypeError: can't convert IntegerMimic into Integer for bitwise arithmetic>
"|"
#<TypeError: can't convert IntegerMimic into Integer for bitwise arithmetic>
"^"
#<TypeError: can't convert IntegerMimic into Integer for bitwise arithmetic>


#=>
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
"+"
:coerce
13
"-"
:coerce
-7
"*"
:coerce
30
"/"
:coerce
0
"%"
:coerce
3
"&"
:to_int
2
"|"
:to_int
11
"^"
:to_int
9

-- 
// SASADA Koichi at atdot dot net