# How to - Change operator behaviour

**URL:** <https://www.ruby-forum.com/t/how-to-change-operator-behaviour/65328>\
**Category:** Ruby\
**Created:** [August 17, 2006, 4:15pm UTC](https://www.ruby-forum.com/t/how-to-change-operator-behaviour/65328 "2006-08-17T16:15:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![henrys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@henrys](https://www.ruby-forum.com/u/henrys)\
**Post date:** [August 17, 2006, 4:15pm UTC](https://www.ruby-forum.com/t/how-to-change-operator-behaviour/65328/1 "2006-08-17T16:15:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![henrys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@henrys](https://www.ruby-forum.com/u/henrys)\
**Post date:** [August 17, 2006, 4:23pm UTC](https://www.ruby-forum.com/t/how-to-change-operator-behaviour/65328/2 "2006-08-17T16:23:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![henrys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@henrys](https://www.ruby-forum.com/u/henrys)\
**Post date:** [August 17, 2006, 7:21pm UTC](https://www.ruby-forum.com/t/how-to-change-operator-behaviour/65328/3 "2006-08-17T19:21:10Z")

</div>

Mark Van H. 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

---

<div class="post-metadata">

**Author:** ![henrys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@henrys](https://www.ruby-forum.com/u/henrys)\
**Post date:** [August 17, 2006, 7:30pm UTC](https://www.ruby-forum.com/t/how-to-change-operator-behaviour/65328/4 "2006-08-17T19:30:08Z")

</div>

ruby keyword: alias

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

Henry S. 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

---

<div class="post-metadata">

**Author:** ![henrys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@henrys](https://www.ruby-forum.com/u/henrys)\
**Post date:** [August 17, 2006, 9:51pm UTC](https://www.ruby-forum.com/t/how-to-change-operator-behaviour/65328/5 "2006-08-17T21:51:16Z")

</div>

Henry S. wrote:

> > ```
> > #do soem comparison
> > 
> > ```
> 
> ```
> # 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

---

<div class="post-metadata">

**Author:** ![henrys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@henrys](https://www.ruby-forum.com/u/henrys)\
**Post date:** [August 17, 2006, 8:21pm UTC](https://www.ruby-forum.com/t/how-to-change-operator-behaviour/65328/6 "2006-08-17T20:21:02Z")

</div>

On 8/17/06, Henry S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> # 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

---

<div class="post-metadata">

**Author:** ![henrys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@henrys](https://www.ruby-forum.com/u/henrys)\
**Post date:** [August 17, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/how-to-change-operator-behaviour/65328/7 "2006-08-17T22:12:18Z")

</div>

On 8/17/06, Henry S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thank you  
> Henry
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Well if you really want to do that here you go  
#!/usr/local/bin/ruby -w

class NilClass  
def \> other  
return true if Time === other  
raise NoMethodError, “undefined method `\>’ for nil:NilClass”  
end  
end

class Time  
alias\_method :old\_greater, :\>  
def \> other  
return true if other.nil?  
old\_greater other  
end  
end

def show bool, text1, text2  
puts bool ? text1 : text2  
end

t1 = Time.new  
t2 = Time.new

show t1 \> t2, “error”, “t1 is not \> t2”  
show t2 \> t1, “t2 \> t1”, “error”  
show t1 \> nil, “t1 \> nil”, “error”  
show nil \> t2, “nil \> t2”, “error”  
nil \> 2

Strange though??

Cheers  
Robert

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![henrys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@henrys](https://www.ruby-forum.com/u/henrys)\
**Post date:** [August 18, 2006, 8:49am UTC](https://www.ruby-forum.com/t/how-to-change-operator-behaviour/65328/8 "2006-08-18T08:49:49Z")

</div>

On 8/17/06, Simen E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > when ("NilClass")
> > 
> > ```
> 
> simplified to  
> case object1  
> when object2  
> triggers object2’s === method with object1 as its sole parameter…

case other

> ```
> when Time
> self > other
> 
> ```

Just to see “How deep is your Stack?”

> ```
> when NilClass
> true
> end
> 
> ```

…therefore Time’s === method is triggered which is defined in Class.  
Unless one un/redefines the “===” method from a Class objetc  
(e.g. class \<\< String undefine\_method :=== ; end ) one can always rely  
on  
=== from classes to check for instance

Cheers  
Robert

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![henrys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@henrys](https://www.ruby-forum.com/u/henrys)\
**Post date:** [August 18, 2006, 7:28am UTC](https://www.ruby-forum.com/t/how-to-change-operator-behaviour/65328/9 "2006-08-18T07:28:43Z")

</div>

On Aug 17, 2006, at 1:21 PM, Henry S. 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
