# How to include a class and create an object?

**URL:** <https://www.ruby-forum.com/t/how-to-include-a-class-and-create-an-object/73056>\
**Category:** Rails\
**Created:** [October 20, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/how-to-include-a-class-and-create-an-object/73056 "2006-10-20T22:01:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![scot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@scot](https://www.ruby-forum.com/u/scot)\
**Post date:** [October 20, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/how-to-include-a-class-and-create-an-object/73056/1 "2006-10-20T22:01:18Z")

</div>

Hi

I have a file in /lib/test\_class.rb whic looks like this

module TestClass  
class TestClass

```
 def mehtod1
 end

 def more methods ....

end

```

end

in one of my controllers I have

require ‘test\_class’

This seems to work, however when I do the next line to create an object  
it fails

test = TestClass.new

Any ideas where I am going wrong?

---

<div class="post-metadata">

**Author:** ![scot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@scot](https://www.ruby-forum.com/u/scot)\
**Post date:** [October 20, 2006, 10:27pm UTC](https://www.ruby-forum.com/t/how-to-include-a-class-and-create-an-object/73056/2 "2006-10-20T22:27:25Z")

</div>

Uh, you have to specify the module name…

test = TestClass::TestClass.new

Ruby may be smart, but it’s not prescient.

Jason

---

<div class="post-metadata">

**Author:** ![scot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@scot](https://www.ruby-forum.com/u/scot)\
**Post date:** [October 20, 2006, 11:37pm UTC](https://www.ruby-forum.com/t/how-to-include-a-class-and-create-an-object/73056/3 "2006-10-20T23:37:29Z")

</div>

Jason R. wrote:

> Uh, you have to specify the module name…
> 
> test = TestClass::TestClass.new
> 
> Ruby may be smart, but it’s not prescient.
> 
> Jason

Exactly.

Remember that when you say:

TestClass.new

you are actually calling the “new” method on the module, not the class.  
To actually get the class you have to dig into it with the “::”  
operator.

TestClass::TestClass.new

However, if you simply want to inlcude the class in your app you dont  
need to wrap it in a module at all. And then you dont need the :: at  
all.

---

<div class="post-metadata">

**Author:** ![scot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@scot](https://www.ruby-forum.com/u/scot)\
**Post date:** [October 21, 2006, 2:11am UTC](https://www.ruby-forum.com/t/how-to-include-a-class-and-create-an-object/73056/4 "2006-10-21T02:11:43Z")

</div>

Alex W. wrote:

> you are actually calling the “new” method on the module, not the class.  
> To actually get the class you have to dig into it with the “::”  
> operator.
> 
> TestClass::TestClass.new

cool thanks, whats the advantage of wrapping the code in a module, i’ve  
always done it, but not sure why?

one last thing

How can I call a function on that class

if I have created an object  
pTest = TestClass::TestClass.new

Should I not be able to do this, if there is a method inside the class  
called test?

pTest.test

if getting the error (private method `test’ called)

---

<div class="post-metadata">

**Author:** ![scot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@scot](https://www.ruby-forum.com/u/scot)\
**Post date:** [October 21, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/how-to-include-a-class-and-create-an-object/73056/5 "2006-10-21T16:47:55Z")

</div>

Please read this:

[http://www.rubycentral.com/book/](http://www.rubycentral.com/book/)

Jason
