alias to module methods or class methods

G

Geert Fannes

------_=_NextPart_001_01C53906.28EDE438
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

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

=20

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?

=20

Greetings,

Geert.

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


------_=_NextPart_001_01C53906.28EDE438--
 
D

David A. Black

Hi --

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top