# Module with a class?

**URL:** <https://www.ruby-forum.com/t/module-with-a-class/178936>\
**Category:** Ruby\
**Created:** [November 13, 2009, 7:48pm UTC](https://www.ruby-forum.com/t/module-with-a-class/178936 "2009-11-13T19:48:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [November 13, 2009, 7:48pm UTC](https://www.ruby-forum.com/t/module-with-a-class/178936/1 "2009-11-13T19:48:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [November 13, 2009, 9:31pm UTC](https://www.ruby-forum.com/t/module-with-a-class/178936/2 "2009-11-13T21:31:05Z")

</div>

On Fri, Nov 13, 2009 at 10:48 AM, Matt B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [November 13, 2009, 11:02pm UTC](https://www.ruby-forum.com/t/module-with-a-class/178936/3 "2009-11-13T23:02:58Z")

</div>

On Fri, Nov 13, 2009 at 9:30 PM, Judson L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [November 14, 2009, 12:14am UTC](https://www.ruby-forum.com/t/module-with-a-class/178936/4 "2009-11-14T00:14:59Z")

</div>

On Sat, Nov 14, 2009 at 12:02 AM, Matt B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [November 14, 2009, 12:02am UTC](https://www.ruby-forum.com/t/module-with-a-class/178936/5 "2009-11-14T00:02:35Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [November 19, 2009, 1:10am UTC](https://www.ruby-forum.com/t/module-with-a-class/178936/6 "2009-11-19T01:10:04Z")

</div>

Worked perfectly, thanks again.
