Module_function :func vs. MyModule.func?

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-forum.com/\.

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...

···

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-forum.com/\.

--
James

James Hunt wrote:

class MyClass
(NoMethodError)

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

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)

···

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

--
Posted via http://www.ruby-forum.com/\.

James Hunt wrote:
>> class MyClass
>> (NoMethodError)
>>
>> So, I'm not seeing any difference.
>> --
>> Posted via http://www.ruby-forum.com/\.
>>
>>
>
> 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:

If you look back at the original code posting, there were two test
sets: The first one (with MyModule) called MyClass.new.hello, whereas
the second one (with MyExplicitModule) called MyClass.new.greet.
Perhaps a bit confusing on my part - I apologize.

MyClass#hello was a public method calling MyClass#greet (a private
method) that was mixed in via the include of MyModule.

> MyExplicitModule.greet
> MyExplicitClass.new.greet

Again, this is the second test set, not the first. My apologies for
the confusion.

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)

Because the last two lines should be:
MyModule.greet
MyClass.new.hello

That being said, I think Logan offered a much better explanation. +1 Logan

···

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

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

--
James