# Strange bit of code

**URL:** <https://www.ruby-forum.com/t/strange-bit-of-code/65104>\
**Category:** Ruby\
**Created:** [August 16, 2006, 6:01am UTC](https://www.ruby-forum.com/t/strange-bit-of-code/65104 "2006-08-16T06:01:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_J.son](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/49beb7/32.png) [@Matthew\_J.son](https://www.ruby-forum.com/u/Matthew_J.son)\
**Post date:** [August 16, 2006, 6:01am UTC](https://www.ruby-forum.com/t/strange-bit-of-code/65104/1 "2006-08-16T06:01:29Z")

</div>

I came across an unusual behavior of remove\_method the other day.  
When I discovered that it didn’t work as I expected I experimented  
until I found a relatively simple example illustrating what happens.  
The show method doesn’t seem to be removed when the example calls  
remove\_method, but it is removed as soon as a show method is defined  
in any module or class, even if is defined in a module or class not  
related in any way to the module it is removed from. Can anyone  
explain why this works the way it does?

Thanks,  
Matthew

module A  
module B  
def show  
“pr”  
end  
end  
end  
class C  
include A::B  
end

p "normal: " \<\< C.new.show

p C.instance\_methods - C.superclass.instance\_methods  
p A::B.instance\_methods  
module A  
module B  
remove\_method :show  
end  
end  
p C.instance\_methods - C.superclass.instance\_methods  
p A::B.instance\_methods

p "shouldn’t work - pr was removed " \<\< C.new.show

module A  
end

p "still shouldn’t work " \<\< C.new.show

module S  
def show  
end  
end

# the next line throws an error

p "defining a show method in any class or module now causes it to  
stop working " \<\< C.new.show

---

<div class="post-metadata">

**Author:** ![Matthew\_J.son](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/49beb7/32.png) [@Matthew\_J.son](https://www.ruby-forum.com/u/Matthew_J.son)\
**Post date:** [August 16, 2006, 8:25am UTC](https://www.ruby-forum.com/t/strange-bit-of-code/65104/2 "2006-08-16T08:25:38Z")

</div>

Hi,

At Wed, 16 Aug 2006 12:58:57 +0900,  
Matthew J.son wrote in [ruby-talk:208741]:

> I came across an unusual behavior of remove\_method the other day.  
> When I discovered that it didn’t work as I expected I experimented  
> until I found a relatively simple example illustrating what happens.  
> The show method doesn’t seem to be removed when the example calls  
> remove\_method, but it is removed as soon as a show method is defined  
> in any module or class, even if is defined in a module or class not  
> related in any way to the module it is removed from. Can anyone  
> explain why this works the way it does?

I think it’s a fixed bug. Can you try it with latest snapshot?

$ grep -C2 rb\_clear\_cache ChangeLog  
Mon Jul 10 09:29:12 2006 Nobuyoshi N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

```
* eval.c (rb_clear_cache_for_undef): clear entries for included
  module. fixed: [ruby-core:08180]

```

---

<div class="post-metadata">

**Author:** ![Matthew\_J.son](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/49beb7/32.png) [@Matthew\_J.son](https://www.ruby-forum.com/u/Matthew_J.son)\
**Post date:** [August 16, 2006, 12:49pm UTC](https://www.ruby-forum.com/t/strange-bit-of-code/65104/3 "2006-08-16T12:49:58Z")

</div>

> I think it’s a fixed bug. Can you try it with latest snapshot?
> 
> $ grep -C2 rb\_clear\_cache ChangeLog  
> Mon Jul 10 09:29:12 2006 Nobuyoshi N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> - eval.c (rb\_clear\_cache\_for\_undef): clear entries for included  
> module. fixed: [ruby-core:08180]
> 
> –  
> Nobu Nakada

Thanks! That does look like the source of it. It makes much more  
sense now… 🙂

Matthew
