Module with classes

D

Durga B.

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
 
D

Dhruva Sagar

[Note: parts of this message were removed to make it a legal post.]

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

Colin Bartlett

[Note: parts of this message were removed to make it a legal post.]

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.

Adding to that, here's a link to the online, somewhat out-of-date but still
useful older version of Programming Ruby:
http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html

Also, does the following help?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top