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

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?

Hi,

···

In message "when will coerce be called?" on 04/07/15, "Xiangyu Yang" <xiangyu.yang@gmail.com> writes:

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.

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 :slight_smile:

···

il 15 Jul 2004 01:38:06 -0700, "Xiangyu Yang" <xiangyu.yang@gmail.com> ha scritto::

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!

gabriele renzi wrote:

If you're working with hardware check out
Aracnet; Internet Services. Hotel bookings online

Ruby Hardware Description Language :slight_smile:

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.

In ruby, the "=" will assign other *object* to a *variable*, and can't
(and shouldn't) be overrided. There are no simple way to modify the
state of an *object* referenced by a *variable*. Using .assign or
overriding other operators to do this job are both ugly. I notice that
someone suggested ":=" operator which can be overrided. I dream of it
all the days :). If I have ":=", I'm sure that my CPU model will save
at least half of the current lines and the program will be much more
natural and clearer.

In short, when you use ruby to model some objects, the ability to
assign/modify the *object* state/value in simple expression way is very
useful. It provides the possibility of running ruby code *directly* to
model other worlds.

Right. But not all object states or values can be changed that way,
and there is no meaningful default operation for such a behaviour.
Thus, it makes more sense to use the appropriate #<< or #assign
mechanism.

-austin

···

On Fri, 16 Jul 2004 10:47:15 +0900, Xiangyu Yang <xiangyu.yang@gmail.com> wrote:

gabriele renzi wrote:
> If you're working with hardware check out
> Aracnet; Internet Services. Hotel bookings online
> Ruby Hardware Description Language :slight_smile:
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.

In ruby, the "=" will assign other *object* to a *variable*, and can't
(and shouldn't) be overrided. There are no simple way to modify the
state of an *object* referenced by a *variable*. Using .assign or
overriding other operators to do this job are both ugly. I notice that
someone suggested ":=" operator which can be overrided. I dream of it
all the days :). If I have ":=", I'm sure that my CPU model will save
at least half of the current lines and the program will be much more
natural and clearer.

In short, when you use ruby to model some objects, the ability to
assign/modify the *object* state/value in simple expression way is very
useful. It provides the possibility of running ruby code *directly* to
model other worlds.

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

In article <cd7bq5$gib@odbk17.prod.google.com>,

gabriele renzi wrote:

If you're working with hardware check out
Aracnet; Internet Services. Hotel bookings online

Ruby Hardware Description Language :slight_smile:

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.

Yes, it's annoying. I wish we had a := op.

In ruby, the "=" will assign other *object* to a *variable*, and can't
(and shouldn't) be overrided. There are no simple way to modify the
state of an *object* referenced by a *variable*. Using .assign or
overriding other operators to do this job are both ugly. I notice that
someone suggested ":=" operator which can be overrided. I dream of it
all the days :). If I have ":=", I'm sure that my CPU model will save
at least half of the current lines and the program will be much more
natural and clearer.

I suggested it a while back for this very reason.

BTW: I missed the parent to this post. Are you using RHDL? I'm the
author, so I would be interested in finding out if anyone is using it.
And if you are using it do you have any suggestions for changes. I'm
considering doing another release one of these days.

In short, when you use ruby to model some objects, the ability to
assign/modify the *object* state/value in simple expression way is very
useful. It provides the possibility of running ruby code *directly* to
model other worlds.

Well, it would make it easier anyway.

phil

···

Xiangyu Yang <xiangyu.yang@gmail.com> wrote:

Xiangyu Yang wrote:

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

Actually I think that = could be used instead.

I have a Variable class that can be used like this:

irb(main):001:0> var = Variable[:x]
=> Variable[:x, #<Binding:0x28b72e8>]
irb(main):002:0> var = 0
=> 0
irb(main):003:0> var += 5; [var, x]
=> [5, 5]
irb(main):004:0> var += 5; [var, x]
=> [10, 10]

Regards,
Florian Gross

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 :slight_smile:

···

il 15 Jul 2004 18:42:29 -0700, "Xiangyu Yang" <xiangyu.yang@gmail.com> ha scritto::

gabriele renzi wrote:

If you're working with hardware check out
Aracnet; Internet Services. Hotel bookings online

Ruby Hardware Description Language :slight_smile:

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.

In article <9e7db91104071518595087c46@mail.gmail.com>,

gabriele renzi wrote:
> If you're working with hardware check out
> Aracnet; Internet Services. Hotel bookings online
> Ruby Hardware Description Language :slight_smile:
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.

In ruby, the "=" will assign other *object* to a *variable*, and can't
(and shouldn't) be overrided. There are no simple way to modify the
state of an *object* referenced by a *variable*. Using .assign or
overriding other operators to do this job are both ugly. I notice that
someone suggested ":=" operator which can be overrided. I dream of it
all the days :). If I have ":=", I'm sure that my CPU model will save
at least half of the current lines and the program will be much more
natural and clearer.

In short, when you use ruby to model some objects, the ability to
assign/modify the *object* state/value in simple expression way is very
useful. It provides the possibility of running ruby code *directly* to
model other worlds.

Right. But not all object states or values can be changed that way,
and there is no meaningful default operation for such a behaviour.
Thus, it makes more sense to use the appropriate #<< or #assign
mechanism.

The problem is that I might want to use #<< as 'shift left' in an HDL (or
use it to append to a list as is it's normal semantics for lists). IF we
had a := operator it would be an optional operator that you could define
for your class or not. If we had := I suspect that the 'meaningful
default operation' would evolve in this direction (lots of folks [newbies
primarily] wonder about overriding '=' which is obviously bad, but a ':='
would allow them to mimic the desired behaviour).

Just to reiterate the proposed usage of ':=', here's an example based on
the Bit class in RHDL:

class Bit #bits can take on the values 1,0,X,Z
  def initialize(value)
    assign(value)
  end

  def assign(value)
    #omitted code to make sure value is a valid value
    @value = value
  end
  
  def :=(value)
    assign(value)
  end
  #lost of other methods ommited
end

x = Bit.new(0)
....later...

x := 'Z' #the meaning seems quite clear
#looks better than:
x.assign 'Z'

#and if I have a BitVector (another class which defines an array of Bit)
#I could then do:

bv = BitVector("1001")
bv << 1 #shift operator, shift left 1 position
....later...
bv := "ZZZZ"

So in the case of a BitVector, if I had a ':=' operator I would then be
able to use '<<' to shift (it makes more sense) and not assignment.

I'd certainly like to see a := operator in upcoming versions of Ruby. The
last time it came up, Matz didn't seem to be too opposed to the idea so
maybe there's hope. :wink:

Phil

···

Austin Ziegler <halostatue@gmail.com> wrote:

On Fri, 16 Jul 2004 10:47:15 +0900, Xiangyu Yang ><xiangyu.yang@gmail.com> wrote:

How do you model a register?