Object#undef_method ???

G

Gavin Sinclair

Folks,

You can remove a method from a class or module using Module#remove_method
and Module#undef_method. What if the method you are trying to get rid of
is a _singleton_ method?

For instance,

module X
def X.foo() 4 end
end

X.foo # -> 4

# Some code goes here.

X.foo # -> NameError: undefined method `foo' for X:module [1]

Please write the code in the placeholder.

[1] Or 'NoMethodError'; I can't tell which is which.

A related question. I want to remove a method and redefine it. Why not
just overwrite it? Because that raises a warning. I don't want to run
the code without warnings, and I don't want to have to fiddle with the
warning level at runtime. Any comments appreciated.

Cheers,
Gavin
 
N

nobu.nokada

Hi,

At Tue, 31 Aug 2004 15:20:04 +0900,
Gavin Sinclair wrote in [ruby-talk:111071]:
module X
def X.foo() 4 end
end

X.foo # -> 4
class << X
remove_method:)foo)
end
X.foo # -> NameError: undefined method `foo' for X:module [1]
 
M

Mauricio Fernández

You can remove a method from a class or module using Module#remove_method
and Module#undef_method. What if the method you are trying to get rid of
is a _singleton_ method?

For instance,

module X
def X.foo() 4 end
end

X.foo # -> 4

# Some code goes here.

class << X; undef_method :foo end
X.foo # -> NameError: undefined method `foo' for X:module [1]

Please write the code in the placeholder.

[1] Or 'NoMethodError'; I can't tell which is which.
=============
 

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

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,413
Latest member
KeiraLight

Latest Threads

Top