# Accessing functions in a module without 'include'

**URL:** <https://www.ruby-forum.com/t/accessing-functions-in-a-module-without-include/108140>\
**Category:** Ruby\
**Created:** [June 29, 2007, 11:25am UTC](https://www.ruby-forum.com/t/accessing-functions-in-a-module-without-include/108140 "2007-06-29T11:25:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [June 29, 2007, 11:25am UTC](https://www.ruby-forum.com/t/accessing-functions-in-a-module-without-include/108140/1 "2007-06-29T11:25:56Z")

</div>

Usually I work with modules like this:

# Defining a module in one file

module M  
def f(x)  
…  
end  
end

# Using the module in another file

require ‘M’  
include M  
f(45)

Sometimes I would find it more convenient to _not_ inject  
the module’s namespace into the user’s namespace, i.e. to  
do it without the include statement. I thought it would  
be easy to qualify the foreign function with the module  
name:

# Using the module in another file

# (this does not work)

require ‘M’  
M::f(45) # Error: undefined method ‘f’

Maybe I’m thinking to Perlish here. Can it be done what I want  
to achieve, and how?

Ronald

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [June 29, 2007, 2:00pm UTC](https://www.ruby-forum.com/t/accessing-functions-in-a-module-without-include/108140/2 "2007-06-29T14:00:16Z")

</div>

On Jun 29, 5:25 am, “Ronald F.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Usually I work with modules like this:

# Defining a module in one file

module M  
module\_function # \<\<\<\<\<\<\<\<\< HERE  
def f(x)  
…  
end  
end

T.

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [June 29, 2007, 3:07pm UTC](https://www.ruby-forum.com/t/accessing-functions-in-a-module-without-include/108140/3 "2007-06-29T15:07:28Z")

</div>

Ronald F. wrote:

> require ‘M’
> 
> # (this does not work)
> 
> require ‘M’  
> M::f(45) # Error: undefined method ‘f’
> 
> Maybe I’m thinking to Perlish here. Can it be done what I want  
> to achieve, and how?

Not sure if it’s what you want, but if you define M as follows it works:

module M  
def self.f(x)  
…  
end  
end

cheers,  
mick

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [July 2, 2007, 11:33am UTC](https://www.ruby-forum.com/t/accessing-functions-in-a-module-without-include/108140/4 "2007-07-02T11:33:22Z")

</div>

> > 
> 
> end  
> Thank you, this works perfectly well!

Ronald

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [July 2, 2007, 11:36am UTC](https://www.ruby-forum.com/t/accessing-functions-in-a-module-without-include/108140/5 "2007-07-02T11:36:22Z")

</div>

> # Defining a module in one file
> 
> module M  
> module\_function # \<\<\<\<\<\<\<\<\< HERE  
> def f(x)  
> …  
> end  
> end

Thank you, this is indeed one solution to my problem (though in the  
end, I’m going to stick with Michael H.’ proposal).

Ronald

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [July 2, 2007, 12:32pm UTC](https://www.ruby-forum.com/t/accessing-functions-in-a-module-without-include/108140/6 "2007-07-02T12:32:26Z")

</div>

On Jul 2, 5:34 am, “Ronald F.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > # Defining a module in one file
> > 
> > module M  
> > module\_function # \<\<\<\<\<\<\<\<\< HERE  
> > def f(x)  
> > …  
> > end  
> > end
> 
> Thank you, this is indeed one solution to my problem (though in the  
> end, I’m going to stick with Michael H.’ proposal).

Don’t mention it 🙂 BTW, Just to make sure you know, you can’t use  
Micheal’s solution if you still want the option of including the  
Module elsewhere. In effect using module\_function is the same as:

```
module M
  def self.f(x)
    ....
  end
  def f(x)
    ....
  end
  private :f
end

```

Probably you’ve already figured that out, but just in case…

T.

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [July 2, 2007, 12:58pm UTC](https://www.ruby-forum.com/t/accessing-functions-in-a-module-without-include/108140/7 "2007-07-02T12:58:30Z")

</div>

> ```
> end
> private :f
> end
> 
> ```

I wasn’t aware that the instance functions would then go private,  
but in my case this would be no problem anyway.

Ronald
