# Module\_function question

**URL:** <https://www.ruby-forum.com/t/module-function-question/48159>\
**Category:** Ruby\
**Created:** [November 15, 2005, 2:23pm UTC](https://www.ruby-forum.com/t/module-function-question/48159 "2005-11-15T14:23:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![carmineaskme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/838e76/32.png) [@carmineaskme](https://www.ruby-forum.com/u/carmineaskme)\
**Post date:** [November 15, 2005, 2:23pm UTC](https://www.ruby-forum.com/t/module-function-question/48159/1 "2005-11-15T14:23:28Z")

</div>

Hi to everyone,

let’s say I’ve the following astonishing module:

* * *

module Mod # contained in the file “mymodule.rb”  
CONST = 1974

def initialize()  
@initialized\_value=892  
end

def metodo1()  
puts “this is metodo1”  
end

class MyModClass  
attr\_reader :mymodclass\_value  
def initialize()  
@mymodclass\_value = 2005  
end  
end

module\_function :metodo1

## end

is it correct that “module\_function :metodo1” should allow me to access  
to “metodo1” the following ways:

* * *

require ‘mymodule’

Mod.metodo1() # This is Ok, and works as expected

class MyClass  
include Mod  
attr\_reader :myclass\_value  
attr\_reader :initialized\_value  
def initialize()  
super()  
@myclass\_value = Mod::CONST  
end  
end

temp = MyClass.new()

## temp.metodo1() # without the module\_function, this works!

Why the line “temp.metodo1()” gives the error:  
“Private method ‘metodo1’ called for…” ?

The Pickaxe book states that using “Module#module\_function” actually  
makes a copy of the methods and not an alias.

Thanks again for your help.  
Regards,  
Carmine

---

<div class="post-metadata">

**Author:** ![carmineaskme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/838e76/32.png) [@carmineaskme](https://www.ruby-forum.com/u/carmineaskme)\
**Post date:** [November 15, 2005, 2:37pm UTC](https://www.ruby-forum.com/t/module-function-question/48159/2 "2005-11-15T14:37:10Z")

</div>

> > > > > “C” == Carmine M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

C\> Why the line “temp.metodo1()” gives the error:  
C\> “Private method ‘metodo1’ called for…” ?

module\_function make the method private (i.e. a method which can’t be  
called with a receiver) and create a public method for the singleton  
class

C\> The Pickaxe book states that using “Module#module\_function” actually  
C\> makes a copy of the methods and not an alias.

The book is trying to say this

module Mod  
def meth  
puts “meth”  
end

```
  module_function :meth

  def meth
     puts "new_meth"
  end

```

end

include Mod

Mod.meth # meth  
meth # new\_meth

if module\_function just created an alias, the 2 calls will give the  
same  
output

Guy Decoux

---

<div class="post-metadata">

**Author:** ![carmineaskme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/838e76/32.png) [@carmineaskme](https://www.ruby-forum.com/u/carmineaskme)\
**Post date:** [November 15, 2005, 2:49pm UTC](https://www.ruby-forum.com/t/module-function-question/48159/3 "2005-11-15T14:49:34Z")

</div>

Hi Guy,  
Clear now.

I do promise I’ll stop bothering with silly questions unless tomorrow 😉

Regards,  
Carmine

---

<div class="post-metadata">

**Author:** ![carmineaskme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/838e76/32.png) [@carmineaskme](https://www.ruby-forum.com/u/carmineaskme)\
**Post date:** [November 15, 2005, 3:32pm UTC](https://www.ruby-forum.com/t/module-function-question/48159/4 "2005-11-15T15:32:45Z")

</div>

Selon Carmine M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi Guy,  
> Clear now.
> 
> I do promise I’ll stop bothering with silly questions unless tomorrow 😉

## Don’t worry, people don’t mind silly questions here, as long as they are on-topic 😉 .

Christophe G…

[http://rainbow.conlang.free.fr](http://rainbow.conlang.free.fr)

It takes a straight mind to create a twisted conlang.
