# Why is this cause an infinite loop?

**URL:** <https://www.ruby-forum.com/t/why-is-this-cause-an-infinite-loop/143352>\
**Category:** Ruby\
**Created:** [July 17, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/why-is-this-cause-an-infinite-loop/143352 "2008-07-17T16:54:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [July 17, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/why-is-this-cause-an-infinite-loop/143352/1 "2008-07-17T16:54:19Z")

</div>

Why is this cause an infinite loop?

class X  
def m; “m”; end  
end

class Y \< X  
def self.method\_added(sym)  
alias\_method("\_\_#{sym}", sym)  
remove\_method(sym)  
end  
end

class Y  
def m; m; end  
end

Y.new.\_\_m

T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [July 17, 2008, 5:01pm UTC](https://www.ruby-forum.com/t/why-is-this-cause-an-infinite-loop/143352/2 "2008-07-17T17:01:22Z")

</div>

2008/7/17 Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Why is this cause an infinite loop?
> 
> class X  
> def m; “m”; end  
> end
> 
> class Y \< X  
> def self.method\_added(sym)

```
   p sym

```

> ```
> alias_method("__#{sym}", sym)
> remove_method(sym)
> 
> ```
> 
> end  
> end
> 
> class Y  
> def m; m; end  
> end
> 
> Y.new.\_\_m

Tom, see above. Creating an alias triggers method\_added.

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [July 17, 2008, 7:29pm UTC](https://www.ruby-forum.com/t/why-is-this-cause-an-infinite-loop/143352/3 "2008-07-17T19:29:03Z")

</div>

On Jul 17, 10:55 am, “Pit C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Y.new.\_\_m
> 
> Tom, see above. Creating an alias triggers method\_added.

Dooh!

Thanks, Pit.

T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [July 17, 2008, 7:36pm UTC](https://www.ruby-forum.com/t/why-is-this-cause-an-infinite-loop/143352/4 "2008-07-17T19:36:40Z")

</div>

On Thu, Jul 17, 2008 at 7:23 PM, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Jul 17, 10:55 am, “Pit C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 2008/7/17 Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> > 
> > > class Y \< X  
> > > def self.method\_added(sym)  
> > > return if /^\_\_/ === sym.to\_s

But you probably figured that out by yourself ;).

Cheers  
Robert

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

* * *

AALST (n.) One who changes his name to be further to the front  
D.Adams; The Meaning of LIFF
