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