# How do I create an instance of a class in another rb file

**URL:** <https://www.ruby-forum.com/t/how-do-i-create-an-instance-of-a-class-in-another-rb-file/67530>\
**Category:** Ruby\
**Created:** [September 5, 2006, 1:22am UTC](https://www.ruby-forum.com/t/how-do-i-create-an-instance-of-a-class-in-another-rb-file/67530 "2006-09-05T01:22:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 5, 2006, 1:22am UTC](https://www.ruby-forum.com/t/how-do-i-create-an-instance-of-a-class-in-another-rb-file/67530/1 "2006-09-05T01:22:38Z")

</div>

Hi

I have two rb files in the same folder.

The first file is one.rb and defines an instanciable class named ‘One’.  
The second file is two.rb and needs to create an instance of class ‘One’  
as defined in one.rb.

How do I import the class ‘One’ into two.rb?

Thanks

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 5, 2006, 1:36am UTC](https://www.ruby-forum.com/t/how-do-i-create-an-instance-of-a-class-in-another-rb-file/67530/2 "2006-09-05T01:36:39Z")

</div>

On Sep 4, 2006, at 7:21 PM, removed\_email\_address@domain.invalid wrote:

> How do I import the class ‘One’ into two.rb?

require ‘one’  
one = One.new

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 5, 2006, 1:54am UTC](https://www.ruby-forum.com/t/how-do-i-create-an-instance-of-a-class-in-another-rb-file/67530/3 "2006-09-05T01:54:40Z")

</div>

On 9/4/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi
> 
> I have two rb files in the same folder.
> 
> The first file is one.rb and defines an instanciable class named ‘One’.  
> The second file is two.rb and needs to create an instance of class ‘One’  
> as defined in one.rb.
> 
> How do I import the class ‘One’ into two.rb?

Assuming the only class defined in one.rb is ‘One’ then you should be  
able  
to do:  
require ‘one’

Thanks

> 

Michael G.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 5, 2006, 2:16am UTC](https://www.ruby-forum.com/t/how-do-i-create-an-instance-of-a-class-in-another-rb-file/67530/4 "2006-09-05T02:16:41Z")

</div>

Hi

Thanks.

I thought ‘require’ only imported files in the Ruby lib folder.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 5, 2006, 2:14am UTC](https://www.ruby-forum.com/t/how-do-i-create-an-instance-of-a-class-in-another-rb-file/67530/5 "2006-09-05T02:14:41Z")

</div>

Hi

What happens if there is more than one class in ‘one.rb’?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 5, 2006, 2:28am UTC](https://www.ruby-forum.com/t/how-do-i-create-an-instance-of-a-class-in-another-rb-file/67530/6 "2006-09-05T02:28:42Z")

</div>

On 9/4/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi
> 
> What happens if there is more than one class in ‘one.rb’?

require ‘one’ will load the entire one.rb file.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 5, 2006, 2:36am UTC](https://www.ruby-forum.com/t/how-do-i-create-an-instance-of-a-class-in-another-rb-file/67530/7 "2006-09-05T02:36:42Z")

</div>

On 9/4/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi
> 
> Thanks.
> 
> I thought ‘require’ only imported files in the Ruby lib folder.

## powerbook:~ michaelguterl$ ri require --------------------------------------------------------- Kernel#require require(string) =\> true or false

```
 Ruby tries to load the library named _string_, returning +true+ if
 successful. If the filename does not resolve to an absolute path,
 it will be searched for in the directories listed in +$:+. If the
 file has the extension ``.rb'', it is loaded as a source file; if
 the extension is ``.so'', ``.o'', or ``.dll'', or whatever the
 default shared library extension is on the current platform, Ruby
 loads the shared library as a Ruby extension. Otherwise, Ruby tries
 adding ``.rb'', ``.so'', and so on to the name. The name of the
 loaded feature is added to the array in +$"+. A feature will not be
 loaded if it's name already appears in +$"+. However, the file name
 is not converted to an absolute path, so that ``+require
 'a';require './a'+'' will load +a.rb+ twice.

    require "my-library.rb"
    require

```

“db-driver”

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 5, 2006, 4:06am UTC](https://www.ruby-forum.com/t/how-do-i-create-an-instance-of-a-class-in-another-rb-file/67530/8 "2006-09-05T04:06:50Z")

</div>

On Sep 4, 2006, at 8:15 PM, removed\_email\_address@domain.invalid wrote:

> Hi
> 
> Thanks.
> 
> I thought ‘require’ only imported files in the Ruby lib folder.

Well it only loads files from paths that are in $: (aka $LOAD\_PATH).  
One of the paths that is in $LOAD\_PATH by default is ‘.’, which is  
the current working directory.
