I read some materials about modules that is modules are combination of
classes and modules so i made a example but it gives error"undefined
method `run' for #<Man:0x2c2bbc0> (NoMethodError)" .I want call run
method in Module1_Class in Module1,is it possible i call run method
using Man class object.
module Module1
class Module1_Class
def run
puts "I'm running! in Module1"
end
def walk
puts "I'm walking a bit briskly!"
end
end
def crawl
puts "I'm so slowwww!"
end
end
class Man
include Module1
def jump
puts "I'm bipedal and I can jump like a fool!"
end
end
mister_man = Man.new
mister_man.run
Just to throw some light on your code, if you did a mister_man.crawl, it
will result in printint out "I'm so slowwww!"
The methods run & walk are defined in a separate class called Module1_Class,
they will not be available with the Man class instances.
Man::Module1_Class however is available since the included module adds that
to Man. Hopefully this should guide you in the right direction.
···
--
Thanks & Regards,
Dhruva Sagar.
On Tue, Dec 7, 2010 at 17:31, Durga B. <bdurgaprasad_nyros@yahoo.com> wrote:
I read some materials about modules that is modules are combination of
classes and modules so i made a example but it gives error"undefined
method `run' for #<Man:0x2c2bbc0> (NoMethodError)" .I want call run
method in Module1_Class in Module1,is it possible i call run method
using Man class object.
module Module1
class Module1_Class
def run
puts "I'm running! in Module1"
end
def walk
puts "I'm walking a bit briskly!"
end
end
def crawl
puts "I'm so slowwww!"
end
end
class Man
include Module1
def jump
puts "I'm bipedal and I can jump like a fool!"
end
end
mister_man = Man.new
mister_man.run
module Module1
# Next section defines a class with the constant Module1_Class;
# this class has to be accessed from outside this module using
# Module1::Module1_Class
# Methods defined in this class are not (?) part of Module1, so
# won't be "visible" in an instance of a class which includes Module1.
class Module1_Class
def run
puts "I'm running!"
end
end
# next section defines
def crawl
puts "I'm so slowwww!"
end
end
class Man
# next line makes things in Module1 "visible" to this class
include Module1
def jump
puts "I'm bipedal and I can jump like a fool!"
end
end
puts
mister_man = Man.new
mister_man.run rescue p $!
#=> #<NoMethodError: undefined method `run' for #<Man:0x284680>>
mister_man.crawl rescue p $!
#=> I'm so slowwww!
mister_man.jump rescue p $!
#=> I'm bipedal and I can jump like a fool!
puts
modmc = Module1::Module1_Class.new
modmc.run rescue p $!
#=> I'm running!
modmc.crawl rescue p $!
#=> #<NoMethodError: undefined method `crawl'
for #<Module1::Module1_Class:0x283860>>
modmc.jump rescue p $!
#=> #<NoMethodError: undefined method `jump'
for #<Module1::Module1_Class:0x283860>>
puts
manmc = Man::Module1_Class.new
manmc.run rescue p $!
#=> I'm running!
manmc.crawl rescue p $!
#=> #<NoMethodError: undefined method `crawl'
for #<Module1::Module1_Class:0x1b505e0>>
manmc.jump rescue p $!
#=> #<NoMethodError: undefined method `jump'
for #<Module1::Module1_Class:0x1b505e0>>
puts
p Module1::Module1_Class
#=> Module1::Module1_Class
p Man::Module1_Class
#=> Module1::Module1_Class
···
On Tue, Dec 7, 2010 at 12:09 PM, Dhruva Sagar <dhruva.sagar@gmail.com>wrote:
You need to read a bit more on how modules work.
Just to throw some light on your code, if you did a mister_man.crawl, it
will result in printint out "I'm so slowwww!"
The methods run & walk are defined in a separate class called
Module1_Class,
they will not be available with the Man class instances.
Man::Module1_Class however is available since the included module adds that
to Man. Hopefully this should guide you in the right direction.