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

···

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

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

···

On 8/17/06, Henry Savr <hsavr@yahoo.com> 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?

Thank you
Henry

--
Posted via http://www.ruby-forum.com/\.

--
Mark Van Holstyn
mvette13@gmail.com
http://lotswholetime.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

···

On 8/17/06, Henry Savr <hsavr@yahoo.com> 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?

Thank you
Henry

--
Posted via http://www.ruby-forum.com/\.

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

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-forum.com/\.

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-forum.com/\.

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

···

On 8/17/06, Henry Savr <hsavr@yahoo.com> 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-forum.com/\.

--
- Simen

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

return super

Ahh, the joys of inheritence

···

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.

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

<snip>

Cheers
Robert

···

On 8/17/06, Simen Edvardsen <toalett@gmail.com> wrote:

On 8/17/06, Henry Savr <hsavr@yahoo.com> wrote:

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