# How to overide "include" method?

**URL:** <https://www.ruby-forum.com/t/how-to-overide-include-method/179153>\
**Category:** Ruby\
**Created:** [November 18, 2009, 4:42pm UTC](https://www.ruby-forum.com/t/how-to-overide-include-method/179153 "2009-11-18T16:42:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexandremutel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a88e4f/32.png) [@alexandremutel](https://www.ruby-forum.com/u/alexandremutel)\
**Post date:** [November 18, 2009, 4:42pm UTC](https://www.ruby-forum.com/t/how-to-overide-include-method/179153/1 "2009-11-18T16:42:46Z")

</div>

I tried several way, but didn’t found any to override the behavior of  
the “include” method.  
I would like to override it’s behavior to accept a string as an argument  
and perform a special include based on that… so I tried something like  
this:

class Module  
def include(module1, \*smth) # :doc:  
puts “In my include #{self}”  
if ( module1.is\_a? String)  
instance\_eval(File.read(module1), module1)  
else  
super module1,\*smth  
end  
end  
end

without any success, this method is nether called.

Any ideas?

---

<div class="post-metadata">

**Author:** ![alexandremutel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a88e4f/32.png) [@alexandremutel](https://www.ruby-forum.com/u/alexandremutel)\
**Post date:** [November 18, 2009, 5:26pm UTC](https://www.ruby-forum.com/t/how-to-overide-include-method/179153/2 "2009-11-18T17:26:16Z")

</div>

On Nov 18, 8:42 am, Alexandre M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I tried several way, but didn’t found any to override the behavior of  
> the “include” method.

# Class methods of classes inherit from instance methods of

# the Class class before they inherit from instance methods of Module.

# [http://phrogz.net/RubyLibs/RubyMethodLookupFlow.png](http://phrogz.net/RubyLibs/RubyMethodLookupFlow.png)

class Class  
def include(\*args)  
p “hi!”  
super  
end  
end

module Bar; end

class Foo  
include Bar #=\> “hi!”  
end

p Foo.ancestors  
#=\> [Foo, Bar, Object, Kernel]

---

<div class="post-metadata">

**Author:** ![alexandremutel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a88e4f/32.png) [@alexandremutel](https://www.ruby-forum.com/u/alexandremutel)\
**Post date:** [November 18, 2009, 6:46pm UTC](https://www.ruby-forum.com/t/how-to-overide-include-method/179153/3 "2009-11-18T18:46:07Z")

</div>

Gavin K. wrote:

> # Class methods of classes inherit from instance methods of
> 
> # the Class class before they inherit from instance methods of Module.
> 
> # [http://phrogz.net/RubyLibs/RubyMethodLookupFlow.png](http://phrogz.net/RubyLibs/RubyMethodLookupFlow.png)

Thanks! Your example is working well until i use it inside a class  
definition.

But when i’m trying to use it from the irb prompt, but the overloaded  
include it’s not called…  
Is there a way to found from where the method is define? ( with  
method(:include).something?), so i know where to define the overload…

For example, if i put the class overloading in a separate file  
class\_includer.rb  
class Class  
def include(\*args)  
p “hi!”  
super  
end  
end

And from the irb :  
require “class\_includer”  
include “test”

The method above is not called. It means that it’s implemented elsewhere  
in this case (outside a class definition, in the top level).

I tried to overload directly the include method in the irb, but it’s not  
working as well, very strange… well, i have probably to understand  
more how scope on self is working!

So from the top level, where i can overload this “include” method?

---

<div class="post-metadata">

**Author:** ![alexandremutel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a88e4f/32.png) [@alexandremutel](https://www.ruby-forum.com/u/alexandremutel)\
**Post date:** [November 19, 2009, 8:39am UTC](https://www.ruby-forum.com/t/how-to-overide-include-method/179153/4 "2009-11-19T08:39:55Z")

</div>

Gavin K. wrote:

> > So from the top level, where i can overload this “include” method?
> 
> Good question. I’m not sure where main fits into the method lookup  
> flow. (But if someone tells me, I’ll add it to the diagram 🙂

Seems that overriding like this is working :

$x = method(:include)  
def self.include(mod)  
puts “Hi”  
$x.call mod  
end

---

<div class="post-metadata">

**Author:** ![alexandremutel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a88e4f/32.png) [@alexandremutel](https://www.ruby-forum.com/u/alexandremutel)\
**Post date:** [November 19, 2009, 6:15am UTC](https://www.ruby-forum.com/t/how-to-overide-include-method/179153/5 "2009-11-19T06:15:58Z")

</div>

On Nov 18, 10:47 am, Alexandre M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Gavin K. wrote:  
> But when i’m trying to use it from the irb prompt, but the overloaded  
> include it’s not called…  
> Is there a way to found from where the method is define? ( with  
> method(:include).something?), so i know where to define the overload…

Apparently not, since method(:include).source\_location returns nil.

But you can see that the include at the top level is custom for main,  
not the same include from modules:

irb(main):001:0\> class Foo; end  
=\> nil

irb(main):002:0\> [method(:include), method(:include).owner]  
=\> [#\<Method: main.include\>, #\<Class:#Object:0x000001008b8e58\>]

irb(main):003:0\> [Foo.method(:include), Foo.method(:include).owner]  
=\> [#\<Method: Class(Module)#include\>, Module]

> So from the top level, where i can overload this “include” method?

Good question. I’m not sure where main fits into the method lookup  
flow. (But if someone tells me, I’ll add it to the diagram 🙂

---

<div class="post-metadata">

**Author:** ![alexandremutel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a88e4f/32.png) [@alexandremutel](https://www.ruby-forum.com/u/alexandremutel)\
**Post date:** [November 19, 2009, 9:20am UTC](https://www.ruby-forum.com/t/how-to-overide-include-method/179153/6 "2009-11-19T09:20:13Z")

</div>

In a cleaner way, it seems to work on the irb level:

class \<\< self  
alias :\_old\_include\_method :include  
def include(mod)  
puts “Hi”  
\_old\_include\_method mod  
end  
end
