Newbie question - how to use multiple source files

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

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

Quoting Ben H. ([email protected]):

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

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.

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 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.

On Dec 30, 6:36 pm, Ben H. [email protected] 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.

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 :slight_smile:

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”):

[email protected] ~/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
[email protected] ~/Desktop/foobar $
[email protected] ~/Desktop/foobar $ ruby driver.rb
hello from a foo
hello from a bar
[email protected] ~/Desktop/foobar $ cat driver.rb
require ‘foo.rb’
require ‘bar.rb’

foo = Foo.new
foo.hello

bar = Bar.new
bar.hello
[email protected] ~/Desktop/foobar $
[email protected] ~/Desktop/foobar $ cat foo.rb
class Foo
def hello
puts “hello from a foo”
end
end
[email protected] ~/Desktop/foobar $
[email protected] ~/Desktop/foobar $ cat bar.rb
class Bar
def hello
puts “hello from a bar”
end
end
[email protected] ~/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

HTH,

Thufir

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs