# How to remove a method from an object?

**URL:** <https://www.ruby-forum.com/t/how-to-remove-a-method-from-an-object/125573>\
**Category:** Ruby\
**Created:** [January 10, 2008, 9:35am UTC](https://www.ruby-forum.com/t/how-to-remove-a-method-from-an-object/125573 "2008-01-10T09:35:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Valerio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@Valerio\_S](https://www.ruby-forum.com/u/Valerio_S)\
**Post date:** [January 10, 2008, 9:35am UTC](https://www.ruby-forum.com/t/how-to-remove-a-method-from-an-object/125573/1 "2008-01-10T09:35:45Z")

</div>

hello everyone,

how can I remove a method from an object, without impacting future  
instances of its class ?

say:

class Server  
def print  
puts ‘hello’  
end  
end

s1 = Server.new  
s1.print #this work

#now remove print from s1 somehow…

s1.print #this should hit method\_missing

s2 = Server.new  
s2.print # this should work fine as well…

Thanks for any help

---

<div class="post-metadata">

**Author:** ![Valerio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@Valerio\_S](https://www.ruby-forum.com/u/Valerio_S)\
**Post date:** [January 10, 2008, 10:20am UTC](https://www.ruby-forum.com/t/how-to-remove-a-method-from-an-object/125573/2 "2008-01-10T10:20:58Z")

</div>

class Server  
attr\_accessor :shh

def initialize(f=false)  
@shh = f  
end

def print  
if @shh  
self.send(:method\_missing)  
else  
puts ‘hi there’  
end  
end

def method\_missing  
puts ‘I look forward to seeing better ways of doing this!’  
end

# 

end

s = Server.new  
s.print

s2 = Server.new(true)  
s2.print

s.print

On Jan 10, 12:32 am, Valerio S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Valerio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@Valerio\_S](https://www.ruby-forum.com/u/Valerio_S)\
**Post date:** [January 10, 2008, 10:59am UTC](https://www.ruby-forum.com/t/how-to-remove-a-method-from-an-object/125573/3 "2008-01-10T10:59:01Z")

</div>

On Jan 10, 2008 4:35 PM, Valerio S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> how can I remove a method from an object, without impacting future  
> instances of its class ?

the method is defined in the class, if you remove it, others will be  
affected.

try using singleton\_method instead…(and yes, you can remove  
singleton methods w your hearts content; but since these methods  
belong to a particular object, you might not even bother removing…  
🙂

kind regards -botp

---

<div class="post-metadata">

**Author:** ![Valerio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@Valerio\_S](https://www.ruby-forum.com/u/Valerio_S)\
**Post date:** [January 10, 2008, 11:13am UTC](https://www.ruby-forum.com/t/how-to-remove-a-method-from-an-object/125573/4 "2008-01-10T11:13:04Z")

</div>

Alle giovedì 10 gennaio 2008, Valerio S. ha scritto:

> end  
> s2.print # this should work fine as well…
> 
> Thanks for any help

This should work:

class Server  
def print  
puts “hello from #{object\_id}”  
end  
end

s1 = Server.new  
s1.print

class \<\< s1  
undef\_method :print  
end

s2 = Server.new  
s2.print  
begin s1.print  
rescue NoMethodError  
puts “undefined method ‘print’ for #{s1.object\_id}”  
end

=\>  
hello from -605995538  
hello from -605995648  
undefined method ‘print’ for -605995538

As you can see from the ids displayed by the print method, after  
removing the  
method from the singleton class of s1, only the call to s2.print works;  
the  
call to s1.print raises a NoMethodError exception.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![Valerio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@Valerio\_S](https://www.ruby-forum.com/u/Valerio_S)\
**Post date:** [January 10, 2008, 11:55am UTC](https://www.ruby-forum.com/t/how-to-remove-a-method-from-an-object/125573/5 "2008-01-10T11:55:16Z")

</div>

Alle giovedì 10 gennaio 2008, botp ha scritto:

> end
> 
> i mean will undef\_ do a remove or what?
> 
> but anyway, that is cool Stefano. thanks for the new i’ve just learned.
> 
> kind regards -botp

Well, in general remove\_method will remove it only from the class where  
it’s  
called. If a superclass defines the same method, the superclass’s method  
will  
be called. According to ri, instead, undef\_method, prevents the class  
from  
responding to the method at all. Here’s an example showing the  
difference

class A  
def test  
puts “test for class A”  
end  
end

class B \< A  
def test  
puts “test for class B”  
end  
end

b = B.new  
b.test  
=\> test for class B

class B  
remove\_method :test  
end

b.test  
=\> test for class A

class B  
undef\_method :test  
end

b.test  
=\> undefined method `test’ for #\<B:0xb7c9a754\> (NoMethodError)

In my previous post, used undef\_method instead of remove\_method thinking  
(without trying it) that remove\_method would only remove it from the  
singleton class. I tried it now, and I saw it works with both methods.  
It  
seems singleton classes are treated in a different way.

Stefano

---

<div class="post-metadata">

**Author:** ![Valerio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@Valerio\_S](https://www.ruby-forum.com/u/Valerio_S)\
**Post date:** [January 10, 2008, 12:19pm UTC](https://www.ruby-forum.com/t/how-to-remove-a-method-from-an-object/125573/6 "2008-01-10T12:19:52Z")

</div>

On Jan 10, 2008 11:54 AM, Stefano C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > class \<\< s1  
> > called. If a superclass defines the same method, the superclass’s method will  
> > def test  
> > end
> 
> In my previous post, used undef\_method instead of remove\_method thinking  
> (without trying it) that remove\_method would only remove it from the  
> singleton class. I tried it now, and I saw it works with both methods.  
> Does it? I cannot reproduce that behavior:

class \<\< Class::new {  
def a; 42 end  
}  
remove\_method :a  
end

singleton.rb:7:in `remove_method': method `a’ not defined in Class  
(NameError)  
from singleton.rb:7

> It  
> seems singleton classes are treated in a different way.  
> IIRC Mauricio F. has shown that undef\_method leaked (or still  
> leaks) so normally I would use remove\_method  
> whenever possible, which is however not the case here.  
> Cheers  
> Robert

–  
[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

Whereof one cannot speak, thereof one must be silent.  
Ludwig Wittgenstein

---

<div class="post-metadata">

**Author:** ![Valerio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@Valerio\_S](https://www.ruby-forum.com/u/Valerio_S)\
**Post date:** [January 10, 2008, 11:35am UTC](https://www.ruby-forum.com/t/how-to-remove-a-method-from-an-object/125573/7 "2008-01-10T11:35:53Z")

</div>

On Jan 10, 2008 6:12 PM, Stefano C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class \<\< s1  
> undef\_method :print  
> end

ah, undef\_,

how does it compare then if i do,  
class \<\< s1  
remove\_method :print  
end

i mean will undef\_ do a remove or what?

but anyway, that is cool Stefano. thanks for the new i’ve just learned.

kind regards -botp

---

<div class="post-metadata">

**Author:** ![Valerio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@Valerio\_S](https://www.ruby-forum.com/u/Valerio_S)\
**Post date:** [January 10, 2008, 3:11pm UTC](https://www.ruby-forum.com/t/how-to-remove-a-method-from-an-object/125573/8 "2008-01-10T15:11:46Z")

</div>

Alle giovedì 10 gennaio 2008, Robert D. ha scritto:

> Does it? I cannot reproduce that behavior:
> 
> class \<\< Class::new {  
> def a; 42 end  
> }  
> remove\_method :a  
> end
> 
> singleton.rb:7:in `remove_method': method`a’ not defined in Class  
> (NameError) from singleton.rb:7

You’re right, it doesn’t work. I didn’t look close enough at the output.

Stefano

---

<div class="post-metadata">

**Author:** ![Valerio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@Valerio\_S](https://www.ruby-forum.com/u/Valerio_S)\
**Post date:** [January 10, 2008, 1:56pm UTC](https://www.ruby-forum.com/t/how-to-remove-a-method-from-an-object/125573/9 "2008-01-10T13:56:12Z")

</div>

Brian.LeRoux ha scritto:

> ```
> else
> 
> ```
> 
> s = Server.new  
> On Jan 10, 12:32ï¿½am, Valerio S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> > ï¿½ ï¿½ ï¿½ ï¿½puts ‘hello’  
> > s2 = Server.new  
> > s2.print # this should work fine as well…
> > 
> > Thanks for any help

Maybe it is not what the original post asked for, but I could not  
resist…

class Server  
def initialize(add\_print\_method=false)  
if (add\_print\_method)  
def self.print  
puts “Hi #{self.object\_id.to\_s(16)}”;  
end  
end

```
puts "Object #{self.object_id.to_s(16)} created"

```

end  
end

a=Server.new(true)  
b=Server.new

a.print # prints “Hi …”  
b.print # raises NoMethodError
