X
Xiangyu Yang
I want to treat "true" as 1 and "false" as 0. So I do:
class TrueClass
def coerce(other)
[other,1]
end
end
class FalseClass
def coerce(other)
[other,0]
end
end
But the code works only for +, -; not for &, |.
irb(main):485:0> 1+true
=> 2
irb(main):486:0> 1|true
TypeError: cannot convert true into Integer
from (irb):486:in `|'
from (irb):486
from :0
Why?
class TrueClass
def coerce(other)
[other,1]
end
end
class FalseClass
def coerce(other)
[other,0]
end
end
But the code works only for +, -; not for &, |.
irb(main):485:0> 1+true
=> 2
irb(main):486:0> 1|true
TypeError: cannot convert true into Integer
from (irb):486:in `|'
from (irb):486
from :0
Why?