# Redef undef'ed methods?

**URL:** <https://www.ruby-forum.com/t/redef-undefed-methods/56454>\
**Category:** Ruby\
**Created:** [March 31, 2006, 9:59am UTC](https://www.ruby-forum.com/t/redef-undefed-methods/56454 "2006-03-31T09:59:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![capnregex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@capnregex](https://www.ruby-forum.com/u/capnregex)\
**Post date:** [March 31, 2006, 9:59am UTC](https://www.ruby-forum.com/t/redef-undefed-methods/56454/1 "2006-03-31T09:59:15Z")

</div>

How “final” is a call to undef\_method? Is there any way to get the  
method back, ie. hook it up to the class again?

From my understanding of the C code it seems impossible since the  
function/method pointer is set to 0 and there is no way from Ruby to  
access the original value for that pointer again (unless you have  
extracted “it” earlier with one of the “\*method” methods). Is this  
analysis correct?

If so code like

module Kernel  
[:eval, :system, :exec, :callcc, :`, #`  
:set\_trace\_func, :sleep, :syscall].each do |m|  
undef\_method m  
end  
end

class Module  
[:module\_eval, :class\_eval,  
:define\_method, :method, :instance\_method,  
:private\_class\_method].each do |m|  
undef\_method m  
end  
end

class Object  
[:instance\_eval].each {|m| undef\_method m}  
end

etc would give a possibility for a kind of simple, fine-grained  
“sandboxing”? (I’m not implying you should do this instead of  
tainting/SAFE-levels etc, just exploring possibilities)

Thanks,

Robert F.

---

<div class="post-metadata">

**Author:** ![capnregex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@capnregex](https://www.ruby-forum.com/u/capnregex)\
**Post date:** [March 31, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/redef-undefed-methods/56454/2 "2006-03-31T16:01:39Z")

</div>

Robert F. wrote:

> How “final” is a call to undef\_method? Is there any way to get the  
> method back, ie. hook it up to the class again?
> 
> […]  
> etc would give a possibility for a kind of simple, fine-grained  
> “sandboxing”? (I’m not implying you should do this instead of  
> tainting/SAFE-levels etc, just exploring possibilities)

\_why is using this already with great success for his TryRuby sandbox.  
I’m not sure if there is ways around it, but it seems quite unlikely.

But you will definitely have to make sure that there is no way of  
loading C extensions. It would be possible to get back the original  
functions that way and do even worse.

---

<div class="post-metadata">

**Author:** ![capnregex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@capnregex](https://www.ruby-forum.com/u/capnregex)\
**Post date:** [March 31, 2006, 4:35pm UTC](https://www.ruby-forum.com/t/redef-undefed-methods/56454/3 "2006-03-31T16:35:31Z")

</div>

On 3/31/06, Florian GroÃ? [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class A  
> alias\_method :b, :a  
> undef\_method :a  
> end

…

class A  
alias\_method :a, :b  
end

Obviously I missed something ;)!

Cheers  
Robert

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![capnregex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@capnregex](https://www.ruby-forum.com/u/capnregex)\
**Post date:** [March 31, 2006, 4:44pm UTC](https://www.ruby-forum.com/t/redef-undefed-methods/56454/4 "2006-03-31T16:44:42Z")

</div>

On 3/31/06, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> …
> 
> class A  
> alias\_method :a, :b  
> end
> 
> Obviously I missed something ;)!

Yes, that will work around. Also you can do it with define\_method if  
you have a handle to the undef’ed method. However, these “holes” can  
be covered by undef’ing more methods (ie alias\_method etc) so it might  
still be a useful technique for some situations.

Thanks,

Robert F.

---

<div class="post-metadata">

**Author:** ![capnregex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@capnregex](https://www.ruby-forum.com/u/capnregex)\
**Post date:** [March 31, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/redef-undefed-methods/56454/5 "2006-03-31T19:14:41Z")

</div>

Robert F. wrote:

> Yes, that will work around. Also you can do it with define\_method if  
> you have a handle to the undef’ed method. However, these “holes” can  
> be covered by undef’ing more methods (ie alias\_method etc) so it might  
> still be a useful technique for some situations.

Or by undefing the methods before anyone has a chance of creating  
aliases or using .instance\_method()? 🙂
