Alias to module methods or class methods

Hello,

I tried to fix the Math.sqrt() domain error on windows I reported an
hour ago by redefining the Math.sqrt method:

module Math

  alias sqrt_old sqrt

  def Math.sqrt(x)

    begin

      Math.sqrt_old(x)

    rescue Errno::EDOM

      return 0.0/0.0

    end

  end

end

But this does not work. Apparently, I cannot create an alias to a module
method. I tested a bit and found out I could not create aliases to class
functions either. How can I create aliases to module methods and class
methods?

Greetings,

Geert.

PS:I found some confusing thread about creating class method aliases so
I ask it here again

Hi --

On Mon, 4 Apr 2005, Geert Fannes wrote:

Hello,

I tried to fix the Math.sqrt() domain error on windows I reported an
hour ago by redefining the Math.sqrt method:

module Math

alias sqrt_old sqrt

def Math.sqrt(x)

   begin

     Math.sqrt_old(x)

   rescue Errno::EDOM

     return 0.0/0.0

   end

end

end

But this does not work. Apparently, I cannot create an alias to a module
method. I tested a bit and found out I could not create aliases to class
functions either. How can I create aliases to module methods and class
methods?

You have to make the change in the class where the method is defined.
Class and module methods are basically singleton methods -- that is,
methods defined inside the singleton class of a particular object. In
this case, the object is the Module object "Math" -- so you need to
open up the singleton class of Math:

   class << Math
     alias sqrt_old sqrt
     def sqrt(x)
       Math.sqrt_old(x)
     rescue Errno::EDOM
       return 0.0/0.0
     end
   end

(Note that a method definition gives you an implicit begin/end block,
so you don't need to write one inside the method.)

David

···

--
David A. Black
dblack@wobblini.net