# Modular classes and avoiding extension clashes

**URL:** <https://www.ruby-forum.com/t/modular-classes-and-avoiding-extension-clashes/107447>\
**Category:** Ruby\
**Created:** [June 21, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/modular-classes-and-avoiding-extension-clashes/107447 "2007-06-21T22:18:38Z")\
**Posts on this page:** 2\
**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:** [June 21, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/modular-classes-and-avoiding-extension-clashes/107447/1 "2007-06-21T22:18:38Z")

</div>

I refer back to:

Ruby-talk: 240334

[http://groups.google.com/group/ruby-talk-google/browse\_thread/thread/9dbe8cd0898227b5/409301042321f6fc?lnk=gst&q=open+class+module+trans&rnum=2&hl=en#409301042321f6fc](http://groups.google.com/group/ruby-talk-google/browse_thread/thread/9dbe8cd0898227b5/409301042321f6fc?lnk=gst&q=open+class+module+trans&rnum=2&hl=en#409301042321f6fc)

I’ve thought of another potential benefit of this approach. While I  
had believed it would be only reasonable to have a single anonymous  
module per class, there may be greater benefit in multiple modules. B/  
c, it may make possible the avoidance of extension clashes. Here’s an  
example:

# friend1.rb

class String  
def exclaim; self + ‘!’; end  
def scream; exclaim + ‘!!’; end  
end  
…

# friend2.rb

class String  
def exclaim; puts self + ‘!’; end  
def scream; exclaim; exclaim; end  
end  
…

# elsewhere.rb

require ‘firend1’  
require ‘firend2’

Clearly there’s a conflict. How can I use friend1#scream but also  
friend2#scream? One’s first thought could well be to alias the method  
so it’s under a different name:

# elsewhere.rb

require ‘firend1’

class String  
alias :friend1scream, :scream  
end

require ‘friend2’

“hey”.firend1scream

However, #exclaim is called by #scream so it will still not work as  
expected.

So getting back to the original suggestion (of the previous thread),  
having open extensions contained in hidden modules… what if method  
lookup followed a route of first checking within the same module, then  
continuing upward before dropping back to ancestors? In other words if  
we had a class, C \< M1 \< M2 and a method was called on it that is  
defined in M2, then any method the M2 method calls would first be  
looked for in M2, then M1, before falling back to Object and Kernel.  
This would resolve the problem with the above example.

It seems to me this is not necessarily unexpected behavior either. If  
I create a method A that depends on a another method B, clearly  
overriding B is dangerous business --it’s something not generally  
done. Usually it’s the “top most” methods --the ones that depend on  
others, that we override. That’s not to say there aren’t cases to do  
so, but in that case a ‘redef’ keyword could be used explicitly.

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:** [June 21, 2007, 10:23pm UTC](https://www.ruby-forum.com/t/modular-classes-and-avoiding-extension-clashes/107447/2 "2007-06-21T22:23:38Z")

</div>

I have ruby in my linux, but i don’t know how do start to ruby for web,  
please how i make it?

2007/6/21, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
