# Curious include behavior

**URL:** <https://www.ruby-forum.com/t/curious-include-behavior/115823>\
**Category:** Ruby\
**Created:** [September 28, 2007, 8:49pm UTC](https://www.ruby-forum.com/t/curious-include-behavior/115823 "2007-09-28T20:49:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [September 28, 2007, 8:49pm UTC](https://www.ruby-forum.com/t/curious-include-behavior/115823/1 "2007-09-28T20:49:06Z")

</div>

This caused me a small headache the other day. By including a module, an  
instance method suddenly becomes accessible as a module method. Seems  
unintuitive, I’m curious to understand the reasoning behind it.

module Foo  
module Bar  
def hello  
‘hello’  
end  
end  
end

Foo::Bar.hello # =\> -:9: undefined method `hello’ for Foo::Bar:Module  
(NoMethodError)

include Foo::Bar

Foo::Bar.hello # =\> “hello”

–  
Tom Preston-Werner

- Libraries:  
Chronic ([chronic.rubyforge.org](http://chronic.rubyforge.org))  
God ([god.rubyforge.org](http://god.rubyforge.org))  
Fuzed ([fuzed.rubyforge.org](http://fuzed.rubyforge.org))
- Site:  
[rubyisawesome.com](http://rubyisawesome.com)

---

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [September 29, 2007, 5:25am UTC](https://www.ruby-forum.com/t/curious-include-behavior/115823/2 "2007-09-29T05:25:36Z")

</div>

David A. Black wrote:

> At the top level, the include will include the module in Object:

Where is the module before it is included?

---

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [September 28, 2007, 9:10pm UTC](https://www.ruby-forum.com/t/curious-include-behavior/115823/3 "2007-09-28T21:10:17Z")

</div>

Hi –

On Sat, 29 Sep 2007, Tom W. wrote:

> end
> 
> Foo::Bar.hello # =\> -:9: undefined method `hello’ for Foo::Bar:Module  
> (NoMethodError)
> 
> include Foo::Bar
> 
> Foo::Bar.hello # =\> “hello”

At the top level, the include will include the module in Object:

ruby -e ‘include Module.new; p Object.ancestors’  
[Object, #Module:0x2592c, Kernel]

whereupon all objects will respond to its methods.

David

---

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [September 29, 2007, 9:07am UTC](https://www.ruby-forum.com/t/curious-include-behavior/115823/4 "2007-09-29T09:07:48Z")

</div>

Hi –

On Sat, 29 Sep 2007, 7stud – wrote:

> David A. Black wrote:
> 
> > At the top level, the include will include the module in Object:
> 
> Where is the module before it is included?

I’m not sure what you mean. Can you elaborate?

David

---

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [September 29, 2007, 10:48am UTC](https://www.ruby-forum.com/t/curious-include-behavior/115823/5 "2007-09-29T10:48:11Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Sat, 29 Sep 2007, 7stud – wrote:
> 
> > David A. Black wrote:
> > 
> > > At the top level, the include will include the module in Object:
> > 
> > Where is the module before it is included?
> 
> I’m not sure what you mean. Can you elaborate?
> 
> David

I would think the name Foo would be visible just like the name of a  
method is visible, e.g.:

def greet  
puts “hello”  
end

According to pickaxe2 p. 346, greet is inserted as a private method of  
Object, and hence can be called in any context without a receiver, i.e.  
without 'some\_obj." in front of the method name.

In the op’s example, there is a module named Foo. Is Foo just a free  
floating object? Or, is it an attribute of some omnipresent object?

---

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [September 29, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/curious-include-behavior/115823/6 "2007-09-29T13:13:50Z")

</div>

Hi –

On Sat, 29 Sep 2007, 7stud – wrote:

> > > Where is the module before it is included?  
> > > puts “hello”  
> > > end
> 
> According to pickaxe2 p. 346, greet is inserted as a private method of  
> Object, and hence can be called in any context without a receiver, i.e.  
> without 'some\_obj." in front of the method name.
> 
> In the op’s example, there is a module named Foo. Is Foo just a free  
> floating object? Or, is it an attribute of some omnipresent object?

Constants you define at the top level are owned by Object:

class C  
end

module M  
end

X = 1

p Object.constants.grep(/^(C|M|X)$/) # =\> [“X”, “C”, “M”]

David

---

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [September 30, 2007, 6:24pm UTC](https://www.ruby-forum.com/t/curious-include-behavior/115823/7 "2007-09-30T18:24:20Z")

</div>

On 9/30/07, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> David A. Black wrote:
> 
> > Constants you define at the top level are owned by Object:
> 
> Is an include always necessary to be able to call a module’s instance  
> methods?

Yes I believe so, that is include or extend

irb(main):001:0\> module M  
irb(main):002:1\> def a; 42 end  
irb(main):003:1\> end  
=\> nil  
irb(main):009:0\> m = M.instance\_method(“a”)  
=\> #\<UnboundMethod: M#a\>  
irb(main):010:0\> m.bind(“a”).call  
TypeError: bind argument must be an instance of M  
from (irb):10:in `bind’  
from (irb):10  
from :0  
irb(main):012:0\> C = Class::new{ include M }  
###################^^^^^^^  
=\> C  
irb(main):013:0\> m.bind(C.new).call  
=\> 42  
irb(main):014:0\> a=“42”  
=\> “42”  
irb(main):015:0\> a.extend M  
###########^^^^^^  
=\> “42”  
irb(main):016:0\> m.bind(a).call  
=\> 42

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [September 30, 2007, 5:10am UTC](https://www.ruby-forum.com/t/curious-include-behavior/115823/8 "2007-09-30T05:10:18Z")

</div>

David A. Black wrote:

> Constants you define at the top level are owned by Object:

Is an include always necessary to be able to call a module’s instance  
methods?
