How to - Change operator behaviour

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

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

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

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

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

On 8/17/06, Henry S. [email protected] 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

On 8/17/06, Henry S. [email protected] wrote:

Thank you
Henry


Posted via 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

On 8/17/06, Simen E. [email protected] 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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs