when will coerce be called?

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?
 
Y

Yukihiro Matsumoto

Hi,

In message "when will coerce be called?"

|I want to treat "true" as 1 and "false" as 0. So I do:
|class TrueClass
|def coerce(other)
|[other,1]
|end
|end

|But the code works only for +, -; not for &, |.
|Why?

"coerce" is only used for numeric operators, not logical operators.
You can redefine "to_int" for the purpose (tough I don't recommend).

matz.
 
G

gabriele renzi

il 15 Jul 2004 01:38:06 -0700, "Xiangyu Yang"
It seems that "coerce" is only used for numeric operators, and "to_int"
only for logical operators, and both not for "~". Then I need code for
~true ....

ps.
Thank matz and your beautiful ruby. I have implemented a SW model for
my crypto processor and its powerful debug environment only in several
hundred lines. It's a productive language!

If you're working with hardware check out
http://www.aracnet.com/~ptkwt/ruby_stuff/RHDL/

Ruby Hardware Description Language :)
 
G

gabriele renzi

il 15 Jul 2004 18:42:29 -0700, "Xiangyu Yang"
An interest thing.

In your RHDL you have to use .assign or .<< to do signal assignment,
annoying thing also in my code for HW CPU model.

I don't like the idea of :=. cause it makes thing little too messy.
Amnyway I'm not the author of RHDL just pointed it out :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top