[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

module_function :func vs. MyModule.func?

7stud 7stud

9/9/2007 5:55:00 AM

The following produce the same output:

module MyModule
def greet
puts "hello"
end

module_function :greet
end

MyModule.greet

#-----vs.-----------

module MyModule
def MyModule.greet
puts "hello"
end
end

MyModule.greet


What is module_function used for?
--
Posted via http://www.ruby-....

13 Answers

7stud 7stud

9/9/2007 6:01:00 AM

0

7stud -- wrote:
> The following produce the same output:
>
> module MyModule
> def greet
> puts "hello"
> end
>
> module_function :greet
> end
>
> MyModule.greet
>
> #-----vs.-----------
>
> module MyModule
> def MyModule.greet
> puts "hello"
> end
> end
>
> MyModule.greet
>
>
> What is module_function used for?


Ahhh, is this it:


module MyModule
def greet
puts "hello"
end
end

module OtherModule
include MyModule

module_function :greet
end

OtherModule.greet

--
Posted via http://www.ruby-....

7stud 7stud

9/9/2007 6:07:00 AM

0

7stud -- wrote:
>
> module MyModule
> def greet
> puts "hello"
> end
> end
>
> module OtherModule
> include MyModule
>
> module_function :greet
> end
>
> OtherModule.greet


On p. 355 of "Programming Ruby (2nd ed)", there is the equivalent of
this example:

module MyModule
def greet
puts "hello"
end

module_function :greet
end

MyModule.greet

As far as I can tell, in that example the module_function has no purpose
since I could just as easily make this change:

module MyModule
def MyModule.greet
puts "hello"
end
end

MyModule.greet

Is there any reason to use module_function in that example?


--
Posted via http://www.ruby-....

James Hunt

9/9/2007 7:37:00 AM

0

On 9/9/07, 7stud -- <dolgun@excite.com> wrote:
> 7stud -- wrote:
> >
> > module MyModule
> > def greet
> > puts "hello"
> > end
> > end
> >
> > module OtherModule
> > include MyModule
> >
> > module_function :greet
> > end
> >
> > OtherModule.greet
>
>
> On p. 355 of "Programming Ruby (2nd ed)", there is the equivalent of
> this example:
>
> module MyModule
> def greet
> puts "hello"
> end
>
> module_function :greet
> end
>
> MyModule.greet
>
> As far as I can tell, in that example the module_function has no purpose
> since I could just as easily make this change:
>
> module MyModule
> def MyModule.greet
> puts "hello"
> end
> end
>
> MyModule.greet
>
> Is there any reason to use module_function in that example?
>
>
> --
> Posted via http://www.ruby-....
>
>

I believe the primary different shows up when the module is included
in another class. Consider this little snippet:

module MyModule
def greet
puts "hello"
end

module_function :greet
end

class MyClass
include MyModule

def hello
greet
end
end

MyModule.greet
MyClass.new.hello

When this gets run, the output will be:
hello
hello

The kicker is that greet is mixed into MyClass as a private method
(try calling MyClass.new.greet and watch the access error).

Using your explicit definition as such:

module MyExplicitModule
def MyExplicitModule.greet
puts "hello, explicitly!"
end
end

class MyExplicitClass
include MyExplicitModule

def hello
greet
end
end

MyExplicitModule.greet
MyExplicitClass.new.greet

The output of this code when run is
hello
module_function.rb:31:in `hello': undefined local variable or method
`greet' for #<MyExplicitClass:0xb7cc7eac> (NameError)
from module_function.rb:36

Essentially, by defining explicitly as MyExplicitModule.greet, the
method becomes a member of the module's singleton class, and does not
get mixed into classes that include it. The module_function call
makes the method passed private and creates a singleton method.

You may also want to check out:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-...

The poster gives a good example of the usage (Math.sqrt)

HTH
--
James

7stud 7stud

9/9/2007 8:00:00 AM

0

James Hunt wrote:
>
> I believe the primary different shows up when the module is included
> in another class.
>
> Using your explicit definition as such:
>
> module MyExplicitModule
> def MyExplicitModule.greet
> puts "hello, explicitly!"
> end
> end
>
> class MyExplicitClass
> include MyExplicitModule
>
> def hello
> greet
> end
> end
>
> MyExplicitModule.greet
> MyExplicitClass.new.greet
>
> The output of this code when run is
> hello
> module_function.rb:31:in `hello': undefined local variable or method
> `greet' for #<MyExplicitClass:0xb7cc7eac> (NameError)
> from module_function.rb:36
>

Using your last example with module_function, I get the same result:

module MyModule
def greet
puts "hello"
end

module_function :greet
end

class MyClass
include MyModule
end

MyModule.greet
MyClass.new.greet


--output:--
hello
r1test.rb:13: private method `greet' called for #<MyClass:0x25490>
(NoMethodError)

So, I'm not seeing any difference.
--
Posted via http://www.ruby-....

James Hunt

9/9/2007 8:40:00 AM

0

On 9/9/07, 7stud -- <dolgun@excite.com> wrote:
> Using your last example with module_function, I get the same result:
>
> module MyModule
> def greet
> puts "hello"
> end
>
> module_function :greet
> end
>
> class MyClass
> include MyModule
> end
>
> MyModule.greet
> MyClass.new.greet
>
>
> --output:--
> hello
> r1test.rb:13: private method `greet' called for #<MyClass:0x25490>
> (NoMethodError)
>
> So, I'm not seeing any difference.
> --
> Posted via http://www.ruby-....
>
>

If you look at my code, the MyClass definition creates an instance
method called hello that calls the private greet method. In the
Explicit example, greet just doesn't exist in MyExplicitClass after
the mixin...

--
James

Trans

9/9/2007 8:54:00 AM

0



On Sep 8, 10:54 pm, 7stud -- <dol...@excite.com> wrote:
> The following produce the same output:
>
> module MyModule
> def greet
> puts "hello"
> end
>
> module_function :greet
> end
>
> MyModule.greet
>
> #-----vs.-----------
>
> module MyModule
> def MyModule.greet
> puts "hello"
> end
> end
>
> MyModule.greet
>
> What is module_function used for?


Allow me to further confuse you:

module MyModule
extend self

def greet
puts "hello"
end
end

MyModule.greet

Now I will explain. I the 1st case, using module_function makes an
actual copy of the instance method and defines it as a singleton
method of the module. In the 2nd case, you never have the instance
method to begin with, you simply defined the singleton method
directly. So to illustrate, the 1st example is equivalent to this:

module MyModule
def greet
puts "hello"
end

def MyModule.greet
puts "hello"
end
end

Now for my last example. In which case, we defined the instance
method, but have also included the module into it's own singleton
space. It is functionally the same as the 1st case but is also dynamic
--if we were to add a new method to MyModule later it would
automatically appear as a module method as well.

HTH,
T.


7stud 7stud

9/9/2007 10:16:00 AM

0

James Hunt wrote:
> On 9/9/07, 7stud -- <dolgun@excite.com> wrote:
>> class MyClass
>> (NoMethodError)
>>
>> So, I'm not seeing any difference.
>> --
>> Posted via http://www.ruby-....
>>
>>
>
> If you look at my code, the MyClass definition creates an instance
> method called hello that calls the private greet method. In the
> Explicit example, greet just doesn't exist in MyExplicitClass after
> the mixin...

As far as I can tell, your private hello method is irrelevant since you
never call the hello method:

> MyExplicitModule.greet
> MyExplicitClass.new.greet

If I add the hello method to my last example that uses module_function,
I get the same output:

module MyModule
def greet
puts "hello"
end

module_function :greet
end

class MyClass
include MyModule

def hello
greet
end
end

MyModule.greet
MyClass.new.greet

--output:--
hello
r1test.rb:18: private method `greet' called for #<MyClass:0x25350>
(NoMethodError)



--
Posted via http://www.ruby-....

7stud 7stud

9/9/2007 10:24:00 AM

0

Trans wrote:
> On Sep 8, 10:54 pm, 7stud -- <dol...@excite.com> wrote:
>> MyModule.greet
>>
>> What is module_function used for?
>
>
> Allow me to further confuse you:
>

Thanks for the response. Unfortunately, you're speaking a language I
can't understand yet. All I can do at this point is look at the output
and try to discern a difference.
--
Posted via http://www.ruby-....

Logan Capaldo

9/9/2007 11:26:00 AM

0

On 9/9/07, 7stud -- <dolgun@excite.com> wrote:

> What is module_function used for?
> --

module_function is used to help give us namespaces. The most visible
example is the Math module. Every method in there is
module_function'ed.

2 * Math.sin( 3.4 ) + Math.exp(24) # boy it sure is tiring typing
Math. all the time
include Math
2 * sin(3.4) + exp(24) # much better
> Posted via http://www.ruby-....
>
>

7stud 7stud

9/9/2007 1:05:00 PM

0

Logan Capaldo wrote:
> On 9/9/07, 7stud -- <dolgun@excite.com> wrote:
>
>> What is module_function used for?
>> --
>
> module_function is used to help give us namespaces. The most visible
> example is the Math module. Every method in there is
> module_function'ed.
>
> 2 * Math.sin( 3.4 ) + Math.exp(24) # boy it sure is tiring typing
> Math. all the time
> include Math
> 2 * sin(3.4) + exp(24) # much better

Ah hah! A difference I can discern:

module MyModule
def MyModule.greet
puts "hello"
end
end

MyModule.greet
#boy it sure is tiring typing MyModule every time I want to say hello

include MyModule
greet

--output:--
hello
r1test.rb:10: undefined local variable or method `greet' for main:Object
(NameError)

--
Posted via http://www.ruby-....