Module with a class?

M

Matt Brooks

I have a file that has a class called "Core" that defines tons of
generic methods which populate tons of @instance variables and has
methods to talk to another machine over tcp, etc....

I have other files, such as hello.rb which has a "Hello" class and
world.rb which has a "World" class. I require these files at the top of
the Core classes file.


The point is I have a 4th ruby script file that requires the "Core"
file, and then makes an object of that Core class, called core. Now the
user will invoke Core methods as normal from this outside file, such as
core.connect_to_widget or core.disconnect_from_widget
It also creates general variables, populates them, opens up a tcp socket
talks to another machine, gets info populates instance variables...etc.

Now the user has decided, in the 4th Ruby script file, that he now needs
a Hello object, so calls core.add_hello_widget inwhich the core method
creates the hello object, @hello = Hello.new.

This hello object has methods which it specifically uses from the hello
class, however it needs to call some methods from the Core file and have
access to its attr_accessor instance variables.

Since the core object originally created this hello object, I want the
4th Ruby script file to be able to call a method such as
core.hello.<method_name_inside_hello_class> where this method might call
a method from within the core class.

I keep getting errors about accessing these variables and methods in the
core file.

I have tried making a module for the hello file, and tried including a
class called hello inside the module hello. And then in the core class
i included this module.

***When a core method creates the hello class as some later point I WANT
it to know hey here is this module which has defined this class hello, I
will make a hello object, and since core is creating this hello object,
go ahead and have access to the variables and methods inside core.

Is this possible??? Seems like module can't have a class?

I may need to reexplain it, but basically want classes in different
files, but want the other classes to have access to 1 core files'
objects instance variables and methods, since this core object will be
creating the other objects of the other classes in the other files....

Thanks
-Matt
 
J

Judson Lester

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

I may need to reexplain it, but basically want classes in different
files, but want the other classes to have access to 1 core files'
objects instance variables and methods, since this core object will be
creating the other objects of the other classes in the other files....
Some example code would help a lot. File boudaries notwithstanding it
sounds like you have something like:

class Hello
def say
puts "Hello"
end
end

class Core
def initialize
@hello=nil
end

attr_reader :hello

def add_hello_widget
@hello= Hello.new
end
end

core = Core.new
core.add_hello_widget
core.hello.say #=> "Hello"

Something like that? If not, what's missing?

Judson
 
J

Jesús Gabriel y Galán

Some example code would help a lot. =A0File boudaries notwithstanding it
sounds like you have something like:

class Hello
=A0def say
=A0 =A0puts "Hello"
=A0end
end

class Core
=A0def initialize
=A0 =A0@hello=3Dnil
=A0end

=A0attr_reader :hello

=A0def add_hello_widget
=A0 =A0@hello=3D Hello.new
=A0end
end

core =3D Core.new
core.add_hello_widget
core.hello.say =A0#=3D> "Hello"

Something like that? =A0If not, what's missing?

If I understood correctly, he wants objects of class Hello to call
methods on the instance of Core which created them. Something like
(untested):

class Hello
def initialize core
@core =3D core
end
def say
puts "hello: #{@core.msg}"
end
end

class Core
def initialize msg
@msg =3D msg
@hello=3Dnil
end
attr_reader :hello, :msg

def add_hello_widget
@hello=3D Hello.new self # pass the instance of core to the hello insta=
nce
end
end

core =3D Core.new "i'm core"
core.add_hello_widget
core.hello.say #=3D> "hello: i'm core"

Hope this helps,

Jesus.
 
M

Matt Brooks

If I understood correctly, he wants objects of class Hello to call
methods on the instance of Core which created them. Something like
(untested):

class Hello
def initialize core
@core = core
end
def say
puts "hello: #{@core.msg}"
end
end

class Core
def initialize msg
@msg = msg
@hello=nil
end
attr_reader :hello, :msg

def add_hello_widget
@hello= Hello.new self # pass the instance of core to the hello
instance
end
end

core = Core.new "i'm core"
core.add_hello_widget
core.hello.say #=> "hello: i'm core"

Hope this helps,

Jesus.

Judson's response would have been correct if the say method in hello
would have called a core method from the instance of core object that
create the hello object...

I think Jesus' response is what I needed, I have not tested it yet...
but I am excited to try this on Monday!!! However, Now how would this
work if both classes are in different files??? I think this is what I
need if they were in same file, so can I just require the Hello file at
the top of core, no module stuff needed if want them in separate
files?!?!

Thanks again Jesus
Matt
 
J

Jesús Gabriel y Galán

I think Jesus' response is what I needed, I have not tested it yet...
but I am excited to try this on Monday!!! However, Now how would this
work if both classes are in different files??? =A0I think this is what I
need if they were in same file, so can I just require the Hello file at
the top of core, no module stuff needed if want them in separate
files?!?!

Yes, you can have class Hello in a different file if you want. In that
file you don't need to require anything.
In the file that contains the Core class you would need to require the
file that contains the Hello class, though, since you are using that
constant in the Core class.

Jesus.
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top