Module with a class?

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

On Fri, Nov 13, 2009 at 10:48 AM, Matt B. [email protected]
wrote:

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

On Fri, Nov 13, 2009 at 9:30 PM, Judson L. [email protected] wrote:

Something like that? If 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 = 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.

On Sat, Nov 14, 2009 at 12:02 AM, Matt B. [email protected]
wrote:

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?!?!

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.

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

Worked perfectly, thanks again.