From: ptkwt@...1.aracnet.com (Phil Tomson) Date: 2002-02-20T04:17:41+09:00 Subject: Re: Operator overloading and multiple arguments In article <20020219102349.X4142@atdesk.com>, Paul Brannan wrote: >On Tue, Feb 19, 2002 at 05:37:44AM +0900, Phil Tomson wrote: >> I'm trying to overload the '<=' operator in a class in order to use it for >> assignment: >> >> class Blah >> def initialize(value=0) >> @value = value >> end >> >> def <=(new_val,time=0) >> puts "time is: #{time}" >> @value = new_val >> end >> end >> >> a = Blah.new >> a<=5 #prints: time is 0 >> a<=(8,10) #results in parse error, doesn't like the ',' >> >> So apparently you can't have more than one argument for an overloaded >> operator (it actually kind of makes sense) - is there any way to do this? > >Oddly enough, > > a.<= 5 > a.<= 8,10 > Interesting. >works, but gives me a creepy feeling in my stomach. Using overloaded >operators for alternative assignment behavior doesn't seem justifiable. >Consider the case where I want to store a Blah object in a binary tree, >and I expect the <= operator to be available for comparing two Blah >objects; what will happen then? Yes, I understand the objection. But '=' can't be overloaded at all, so I wanted to use another symbol. Also, what I'm doing is creating what I call RHDL (Ruby Hardware Description Language) and I wanted it to look as much as possible like VHDL which uses '<=' for 'assignment' (it's not really assignment, but it'd take too long to get into that). Anyway, I think it won't be a problem using '<=' for assignment in this case. Here's why: RHDL has Signal objects which contain Bit objects (A Signal has-a Bit). The Bit object carries the value of the signal so comparisons would be done in the Bit class and '<=' would mean the normal "less-than-or-equal" there. However at the Signal level, I'll have to define some kind of 'lte' method. [Of course the obvious question is: Why not just do everything at the Bit level and do away with Signal altogether? - Because there could be other types of value objects which a Signal could have which would behave differently. Right now there's only Bit, but there could be Bit_vector, integers, or some kindof multi-valued logic system.] Phil