# Newbie question - how to use multiple source files

**URL:** <https://www.ruby-forum.com/t/newbie-question-how-to-use-multiple-source-files/124592>\
**Category:** Ruby\
**Created:** [December 30, 2007, 6:37pm UTC](https://www.ruby-forum.com/t/newbie-question-how-to-use-multiple-source-files/124592 "2007-12-30T18:37:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [December 30, 2007, 6:37pm UTC](https://www.ruby-forum.com/t/newbie-question-how-to-use-multiple-source-files/124592/1 "2007-12-30T18:37:11Z")

</div>

I’m coming from Java, and am trying to understand how to break upo code  
into more than one file. So in this example, I have one class in the  
first file, another in the second file, and I want to create an object  
from the first class, but from the second file. Environment - Aptana on  
XP machine. See below:

1. First file named first\_class\_file.rb:

class FirstClassFile

public

def initialize  
super  
puts “Hello from First Class”  
end  
end

my1stObject = FirstClassFile.new

1. Second file named second\_class\_file.rb:

class SecondClassFile

def initialize  
super  
puts “Hello from Second Class”  
end  
end

my2ndObject = SecondClassFile.new

my3rdObject = FirstClassFile.new

1. Console result of running second file:

Hello from Second Class  
second\_class\_file.rb:11: uninitialized constant FirstClassFile  
(NameError)

Question - What obvious thing am I doing wrong?  
Thanks!

- Ben

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [December 30, 2007, 6:42pm UTC](https://www.ruby-forum.com/t/newbie-question-how-to-use-multiple-source-files/124592/2 "2007-12-30T18:42:29Z")

</div>

Subject: newbie question - how to use multiple source files  
Date: lun 31 dic 07 02:36:28 +0900

Quoting Ben H. (removed\_email\_address@domain.invalid):

> I’m coming from Java, and am trying to understand how to break upo code  
> into more than one file. So in this example, I have one class in the first  
> file, another in the second file, and I want to create an object from the  
> first class, but from the second file. Environment - Aptana on XP machine.

In the second file, before referring to the FirstClassFile class, you  
must add

require ‘first\_class\_file’

and first\_class\_file.rb has to be either in your current directory, or  
in a directory included in your search path.

Carlo

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [December 30, 2007, 6:44pm UTC](https://www.ruby-forum.com/t/newbie-question-how-to-use-multiple-source-files/124592/3 "2007-12-30T18:44:15Z")

</div>

Ben H. wrote:

> I’m coming from Java, and am trying to understand how to break upo code  
> into more than one file. So in this example, I have one class in the  
> first file, another in the second file, and I want to create an object  
> from the first class, but from the second file. Environment - Aptana on  
> XP machine. See below:

Check out the “require” and “load” Kernel methods. The “require” method  
is what you want, but “load” also has its uses.

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [December 30, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/newbie-question-how-to-use-multiple-source-files/124592/4 "2007-12-30T19:40:37Z")

</div>

The second file must require the first:

require ‘first\_class\_file’

This would generally go at the top of the file, but anywhere before  
FirstClassFile is referenced is fine. The require method looks at  
each path in the $LOAD\_PATH global variable, then attempts to find a  
Ruby source file or C extension that matches the name you pass in.

Check out [http://www.rubycentral.com/pickaxe/tut\_modules.html#S4](http://www.rubycentral.com/pickaxe/tut_modules.html#S4) for  
more details.

In your example, the public and super calls are unnecessary. Public  
is the default visibility, and, as both of your classes inherit from  
Object, there’s no need to call the initializer of the superclass.  
It’s also quite common to have multiple Ruby classes in the same  
source file. Ruby classes are often small, and many people find it  
easier to manage a small handful of larger files than dozens of tiny  
ones.

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [December 30, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/newbie-question-how-to-use-multiple-source-files/124592/5 "2007-12-30T23:10:38Z")

</div>

On Dec 30, 6:36 pm, Ben H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> public
> 
> my3rdObject = FirstClassFile.new
> 
> 1. Console result of running second file:
> 
> Hello from Second Class  
> second\_class\_file.rb:11: uninitialized constant FirstClassFile (NameError)
> 
> Question - What obvious thing am I doing wrong?  
> Thanks!
> 
> - Ben

Coming from Java I presume that you are familiar with import  
directive.  
Ruby has “require” for the same job as stated before.

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [December 30, 2007, 11:22pm UTC](https://www.ruby-forum.com/t/newbie-question-how-to-use-multiple-source-files/124592/6 "2007-12-30T23:22:55Z")

</div>

On Mon, 31 Dec 2007 02:36:28 +0900, Ben H. wrote:

> So in this example, I have one class in the first file, another in the  
> second file, and I want to create an object from the first class,

Hi 🙂

I’m coming from java, too. Heads up: one of the big but silly  
differences is to not capitalize file names, like Foo.rb, instead call  
it  
foo.rb.

How about something like the following (note the output of “ruby  
driver.rb”):

thufir@arrakis ~/Desktop/foobar $ ll  
total 12  
-rw-r–r-- 1 thufir users 53 Dec 30 14:17 bar.rb  
-rw-r–r-- 1 thufir users 86 Dec 30 14:18 driver.rb  
-rw-r–r-- 1 thufir users 53 Dec 30 14:17 foo.rb  
thufir@arrakis ~/Desktop/foobar $  
thufir@arrakis ~/Desktop/foobar $ ruby driver.rb  
hello from a foo  
hello from a bar  
thufir@arrakis ~/Desktop/foobar $ cat driver.rb  
require ‘foo.rb’  
require ‘bar.rb’

foo = Foo.new  
foo.hello

bar = Bar.new  
bar.hello  
thufir@arrakis ~/Desktop/foobar $  
thufir@arrakis ~/Desktop/foobar $ cat foo.rb  
class Foo  
def hello  
puts “hello from a foo”  
end  
end  
thufir@arrakis ~/Desktop/foobar $  
thufir@arrakis ~/Desktop/foobar $ cat bar.rb  
class Bar  
def hello  
puts “hello from a bar”  
end  
end  
thufir@arrakis ~/Desktop/foobar $

The driver, driver.rb, does all the work.

You may be interested in the code at:  
[http://code.google.com/p/dwemthys/source](http://code.google.com/p/dwemthys/source)

HTH,

Thufir
