[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to - Change operator behaviour

Henry Savr

8/17/2006 2:15:00 PM

Dear Ruby gurus:

I want to write anywhere in my code t1 > t2 and get:
- true if at least one parameter is nil and the other parameter is
Time
- standard comparision result in other case


How to do that?

Thank you
Henry

--
Posted via http://www.ruby-....

6 Answers

Jeff Schwab

8/17/2006 2:41:00 PM

0

Henry Savr wrote:
> Dear Ruby gurus:
>
> I want to write anywhere in my code t1 > t2 and get:
> - true if at least one parameter is nil and the other parameter is
> Time
> - standard comparision result in other case
>
>
> How to do that?

Are you saying that if t1 is nil, but t2 is of type Time, you want both
t1>t2 and t2>t1?

Henry Savr

8/17/2006 5:21:00 PM

0

Mark Van Holstyn wrote:
> In your example, you would be calling the method '>' on t1...
>
> t1.>(t2)
>
> You can define > in the class you would like to have your functionality
> in.
>
> class Example
> def >( other )
> #do soem comparison
> end
> end


Sorry, I see, that my initial question was not asked clearly.
I know about the "> trick."

The question actually was:

I want to redefine a Time method >

It's new behaviour should be like this:

class Time1 < Time
def >(other)
case other.class.name
when "Time"
return self > other # but use the OLD > method definition.
when ("NilClass")
return true
else
# do what Ruby does originally in this case : when one operand is
Time, and other not the Time.
end
end

So question was
1. How to instruct Ruby to use OLD definition?
2. a)What Ruby does if it sees one parameter of Time class and other
non-Time?
b)How to say him to do the same?
Thank you

Henry

--
Posted via http://www.ruby-....

Ilan Berci

8/17/2006 5:30:00 PM

0

ruby keyword: alias

you can alias a previous method and use it in your new def

Henry Savr wrote:

>
> So question was
> 1. How to instruct Ruby to use OLD definition?
> 2. a)What Ruby does if it sees one parameter of Time class and other
> non-Time?
> b)How to say him to do the same?
> Thank you
>
> Henry


--
Posted via http://www.ruby-....

Simen

8/17/2006 6:19:00 PM

0

On 8/17/06, Henry Savr <hsavr@yahoo.com> wrote:
> It's new behaviour should be like this:
>
> class Time1 < Time
> def >(other)
> case other.class.name
> when "Time"
> return self > other # but use the OLD > method definition.
> when ("NilClass")
> return true
> else
> # do what Ruby does originally in this case : when one operand is
> Time, and other not the Time.
> end
> end

Unrelated hat tip: many classes define the #=== method, for "case
equality", and it's used in case statements. Your code could be
simplified to

case other
when Time
self > other
when NilClass
true
end


>
> So question was
> 1. How to instruct Ruby to use OLD definition?
> 2. a)What Ruby does if it sees one parameter of Time class and other
> non-Time?
> b)How to say him to do the same?
> Thank you
>
> Henry
>
> --
> Posted via http://www.ruby-....
>
>


--
- Simen

Daniel Schierbeck

8/17/2006 7:49:00 PM

0

Henry Savr wrote:
> Mark Van Holstyn wrote:
>> In your example, you would be calling the method '>' on t1...
>>
>> t1.>(t2)
>>
>> You can define > in the class you would like to have your functionality
>> in.
>>
>> class Example
>> def >( other )
>> #do soem comparison
>> end
>> end
>
>
> Sorry, I see, that my initial question was not asked clearly.
> I know about the "> trick."
>
> The question actually was:
>
> I want to redefine a Time method >
>
> It's new behaviour should be like this:
>
> class Time1 < Time
> def >(other)
> case other.class.name
> when "Time"
> return self > other # but use the OLD > method definition.
> when ("NilClass")
> return true
> else
> # do what Ruby does originally in this case :
> # when one operand is Time, and other not the Time.
> end
> end

class Time
def > other
if other.nil?
true
else
(self <=> other) == 1
end
end
end


Cheers,
Daniel

Logan Capaldo

8/18/2006 5:28:00 AM

0


On Aug 17, 2006, at 1:21 PM, Henry Savr wrote:

> class Time1 < Time
> def >(other)
> case other.class.name
> when "Time"
> return self > other # but use the OLD > method definition.
return super

Ahh, the joys of inheritence