You only include the module, therefore any methods defined in it will
only be instance methods. By trying to call print_test from inside the
class definition you are calling it as a class method. If you want
print_test to also be a class method you must extend TestMod as well
as including it.
Basically include adds the module’s methods as instance methods and
extend adds them as class methods.
On Tue, Apr 03, 2007 at 06:02:41AM +0900, Ken M. wrote:
end
->test.rb:22: undefined local variable or method ‘print_test’ for
Global:Class
Whether “Global” is a Class or a Method the same happens. However, if I
comment out “Global” completely, out put is as expected:
->test1
->test2
If you ‘include’ a module in a class, its methods are added as instance
methods for objects of that class. Try:
a = Global.new
a.print_test
Thank you two! See, I knew it would be simple and now it even makes
sense.
So in the context of the above, my application will be performing a wide
range of work and will need to be quite modular (ok I’m lazy). As far
as preference and “best practice”, how would I best layout the
namespace. For instance:
I have what I feel should be a simple issue. Look at the following:
############################
Test of nested modules/classes
############################
class Global
module TestMod
TEST = “test1”
def print_test
puts “test2”
end
end#module TestMod
include TestMod
puts TEST
print_test
end#class Global
##############################
->test1
->test.rb:22: undefined local variable or method ‘print_test’ for
Global:Class
Whether “Global” is a Class or a Method the same happens. However, if I
comment out “Global” completely, out put is as expected:
->test1
->test2
Thanks in advance.
irb(main):004:0> class Global
irb(main):005:1> module TestMod
irb(main):006:2> TEST = “test1”
irb(main):007:2> def print_test
irb(main):008:3> puts “test2”
irb(main):009:3> end
irb(main):010:2> end
irb(main):011:1> extend TestMod
irb(main):012:1> end
=> Global
irb(main):013:0> Global.print_test
test2
=> nil
->test.rb:22: undefined local variable or method ‘print_test’ for
Global:Class
Whether “Global” is a Class or a Method the same happens. However, if I
comment out “Global” completely, out put is as expected:
->test1
->test2
Thanks in advance.
When you include TestMod in the class Global, the instance methods of
TestMod
become instance methods of Global, that is, they become methods of the
instances of Global, not of Global itself. So, you can do:
Global.new.print_test
=> “test2”
The same happens if Global were a module.
If you want to add the instance methods of TestMod to Global itself,
instead,
you can do the following:
class Global
module TestMod
…
end #End of TestMod
extend TestMod
end
While Module#include adds the instance methods of the module as instance
methods of the class (or module) where it’s called, extend adds the
instance
methods of the module to its receiver (self, in this case).
Where is the mailing list located. I cannot find it, only these forums.
It seems that some of you guys are using maillist functionality
Hi Ken,
The Ruby Forum is useful for the occasional post. It forwards your
messages to the ruby-talk mailing list. If you would like to subscribe
to ruby-talk go here: