Thank you, things are working now as they should
Greetings,
Geert.
···
-----Original Message-----
From: dblack@wobblini [mailto:dblack@wobblini] On Behalf Of David A.
Black
Sent: 04 April 2005 14:30
To: ruby-talk ML
Subject: Re: alias to module methods or class methods
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