# Problem with ruby classes

**URL:** <https://www.ruby-forum.com/t/problem-with-ruby-classes/159233>\
**Category:** Ruby\
**Created:** [February 2, 2009, 5:23pm UTC](https://www.ruby-forum.com/t/problem-with-ruby-classes/159233 "2009-02-02T17:23:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![manisha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@manisha](https://www.ruby-forum.com/u/manisha)\
**Post date:** [February 2, 2009, 5:23pm UTC](https://www.ruby-forum.com/t/problem-with-ruby-classes/159233/1 "2009-02-02T17:23:10Z")

</div>

Can Anyone please tell me how can we create an object of a class that is  
present in one ruby file, in another ruby file? I tried using .new()  
method but that doesn’t seem to work.It simply cannot find the required  
file.  
It works when I create an object of class inside the same file ,but not  
in another file.

For example , lets say there is a file , file1.rb which contains class  
File1  
there is another file named file2.rb , that contains another class File2

I want to use the methods in file1.rb inside class File2 in file2.rb  
Can someone tell how can I do that?

My codes are somewhat like this

# file1.rb

class File1  
def method1  
end  
end

#file2.rb  
object = File1.new  
class File2  
end

The error while running file2.rb is

file2.rb:3: uninitialized constant File1 (NameError)

---

<div class="post-metadata">

**Author:** ![manisha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@manisha](https://www.ruby-forum.com/u/manisha)\
**Post date:** [February 2, 2009, 5:25pm UTC](https://www.ruby-forum.com/t/problem-with-ruby-classes/159233/2 "2009-02-02T17:25:57Z")

</div>

Alle Monday 02 February 2009, Manisha T. ha scritto:

> #file2.rb  
> object = File1.new  
> class File2  
> end
> 
> The error while running file2.rb is
> 
> file2.rb:3: uninitialized constant File1 (NameError)

Add

require ‘file1’

somewhere before the call to File1.new in file2.rb

Stefano

---

<div class="post-metadata">

**Author:** ![manisha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@manisha](https://www.ruby-forum.com/u/manisha)\
**Post date:** [February 2, 2009, 5:34pm UTC](https://www.ruby-forum.com/t/problem-with-ruby-classes/159233/3 "2009-02-02T17:34:03Z")

</div>

Stefano C. wrote:

> Alle Monday 02 February 2009, Manisha T. ha scritto:
> 
> > #file2.rb  
> > object = File1.new  
> > class File2  
> > end
> > 
> > The error while running file2.rb is
> > 
> > file2.rb:3: uninitialized constant File1 (NameError)
> 
> Add
> 
> require ‘file1’
> 
> somewhere before the call to File1.new in file2.rb
> 
> Stefano

Thanks Stefano !!!  
I have already tried to use require but of no use.It says no file to  
load, where as both the files are in the same directory

---

<div class="post-metadata">

**Author:** ![manisha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@manisha](https://www.ruby-forum.com/u/manisha)\
**Post date:** [February 3, 2009, 3:30am UTC](https://www.ruby-forum.com/t/problem-with-ruby-classes/159233/4 "2009-02-03T03:30:56Z")

</div>

Make sure you don’t include the extension

Sent from my iPhone

On 03/02/2009, at 3:33 AM, Manisha T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
