Hi folks,
as stated in the pickaxe book Module#module_function serves the purpose of
making instance methods defined in a module available also as its singletons.
I just found a nice and elegant alternative: a module can extend itself!
module LR
def lrf()
puts "I am func. LR#lrf"
end
extend self #<<<| extend self by itself!!
end
# trying out
include LR
lrf #-> I am func. LR#lrf
LR.lrf #-> I am func. LR#lrf
# redefine instance func. LR#lrf
module LR
def lrf()
puts "redefined func. LR#lrf"
end
end
# trying out after redefining LR#lrf()
lrf #-> redefined func. LR#lrf
LR.lrf #-> redefined func. LR#lrf
You can see from the example above that the singleton LR.lrf is a reference to
an instant method lrf and that the changes to the instant method are seen
through that singleton. This to be contrasted with the Module#module_function,
which first creates (deep) copies of all the specified instance methods and
makes these copies into singletons. Thus, redefining instance method has no
impact on the singleton if created with module_function.
Cool, yah!!
--Leo-- [ slonika AT yahoo DOT com ]