from :0
------------------------------------------------------------------------
Creates module functions for the named methods. These functions
may be called with the module as a receiver, and also become
available as instance methods to classes that mix in the module.
Module functions are copies of the original, and so may be changed
independently. The instance-method versions are made private. If
used with no arguments, subsequently defined methods become module
functions.
=> true
Thanks for all the help. Here is a summary of the problem and solution:
module Test
def Test.show
puts "testing"
end
def f
puts "goodbye"
end
end
Test.show #testing
Test.f #error
include Test
f #goodbye
show #error
As the example above shows, there is no way to achieve both of the
following:
1) When the Test module isn't included in order to avoid potential name
clashes of Test's methods with other methods having the same name, being
able to call all the methods using the syntax: Test.name.
2) When the Test module is included in order to avoid having to to type
the module name in front of the method names and name clashes aren't a
concern, being able to call all the methods using the syntax: name.
module_function allows you to achieve both 1 and 2:
module Test
def show
puts "testing"
end
def f
puts "goodbye"
end
On Behalf Of 7stud --:
# 1) When the Test module isn't included in order to avoid
# potential name
# clashes of Test's methods with other methods having the same
# name, being
# able to call all the methods using the syntax: Test.name.
···
#
# 2) When the Test module is included in order to avoid having
# to to type
# the module name in front of the method names and name clashes
# aren't a
# concern, being able to call all the methods using the syntax: name.
do not forget third use of module_function since it's one of the nifty features..
3) being able to change the module methods (or module "functions" so to speak) independently of the original methods. Remember, module functions are copies of the original,
ergo you can just modify the copies...
irb(main):050:0> module Test
irb(main):051:1> def show
irb(main):052:2> puts "This is a new show"
irb(main):053:2> end
irb(main):054:1> def f
irb(main):055:2> puts "This is a new goodbye"
irb(main):056:2> end
irb(main):057:1> end
=> nil
do not forget third use of module_function since it's one of the nifty
features..
3) being able to change the module methods (or module "functions" so to
speak) independently of the original methods. Remember, module functions
are copies of the original,
ergo you can just modify the copies...
A new twist! I'll quote your original example:
(I guess I won't--it's too long)
How does that work? You add a new instance method with the same name
as an existing instance method to the module, and somehow the new
instance method
overwrites the old one, and the old instance method gets promoted to a
"class" method?