How do module functions work?

Rubyists,

Why do you have to use Module#module_function in order to be able to call

Foo::bar()

?

And why does it create a copy of the method? I don’t understand the mechanics
at play here.

···


Gavin Sinclair Software Engineer
Sydney, Australia Soyabean Software Pty Ltd

Hi,

···

In message “How do module functions work?” on 02/11/13, “Gavin Sinclair” gsinclair@soyabean.com.au writes:

Why do you have to use Module#module_function in order to be able to call

Foo::bar()

?

And why does it create a copy of the method? I don’t understand the mechanics
at play here.

Instance methods and class methods (or module method) are totally
different entities, so that you have to copy instance methods to make
them callable as class methods.

						matz.

Why do you have to use Module#module_function in order to be able to call

Foo::bar()

?

And why does it create a copy of the method? I don’t understand the
mechanics
at play here.

Instance methods and class methods (or module method) are totally
different entities, so that you have to copy instance methods to make
them callable as class methods.

So how does it get copied? Can it be expressed in Ruby (like attr_accessor),
or is it more magical that that.

matz.

Gavin

···

From: “Yukihiro Matsumoto” matz@ruby-lang.org

module Foo
def bar
“Foo::bar”
end
end

The only way you can get at this Foo#bar is to include it (which
strips the module as a namespace). The two definitions below are
functionally equivalent:

module Foo
def bar
“Foo::bar”
end
def Foo.bar
“Foo::bar”
end
end

module Foo
def bar
“Foo::bar”
end
module_function :bar
end

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.11.13 at 09.47.56

···

On Wed, 13 Nov 2002 18:41:50 +0900, Gavin Sinclair wrote:

From: “Yukihiro Matsumoto” matz@ruby-lang.org

Why do you have to use Module#module_function in order to be
able to call
Foo::bar()

And why does it create a copy of the method? I don’t understand
the mechanics at play here.
Instance methods and class methods (or module method) are totally
different entities, so that you have to copy instance methods to
make them callable as class methods.
So how does it get copied? Can it be expressed in Ruby (like
attr_accessor), or is it more magical that that.

Hi,

···

In message “Re: How do module functions work?” on 02/11/13, “Gavin Sinclair” gsinclair@soyabean.com.au writes:

So how does it get copied? Can it be expressed in Ruby (like attr_accessor),
or is it more magical that that.

You cannot copy methods in pure Ruby.

						matz.

The two definitions below are functionally equivalent:

Well, not really

pigeon% cat b.rb
#!/usr/bin/ruby
module M
   def b
      "b"
   end
   def self.b
      "M::b"
   end
end

class C
   include M
end

p C.new.b
pigeon%

pigeon% b.rb
"b"
pigeon%

  module Foo
    def bar
      "Foo::bar"
    end
    module_function :bar
  end

Try the same with this

Guy Decoux

True. I find it an odd side-effect, but it is documented.

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.11.13 at 09.47.56

···

On Thu, 14 Nov 2002 00:10:25 +0900, ts wrote:

The two definitions below are functionally equivalent:
Well, not really[…]