IRB and rb files

As I make my way through the Pragmatic Programmer’s Guide I am puzzled
by the following issue:

If I type in a Ruby program such as

class Song
def initialize(name, artist, duration)
@name = name
@artist = artist
@duration = duration
end
end

And save it as Song.rb in a directory on my hard drive reserved for
Ruby code, I wonder how I can use it from irb, if possible. I know
that I can type in each line of the .rb program within irb and have it
work when I do song = Song.new(“Bicylops”, “Fleck”, 260)
But if I attempt to type this in from irb after defining the class in a
separate .rb file I get
irb(main):001:0> song = Song.new(“Bicylops”, “Fleck”, 260)
NameError: uninitialized constant Song
from (irb):1
from :0

What I would like to know is, how can I use Ruby P.s which are
already created as source within irb… or is there a better way to run
ruby programs

Hello,

On 8/12/06, ClassRubyExceptionHandline [email protected] wrote:

@duration = duration

NameError: uninitialized constant Song
from (irb):1
from :0

What I would like to know is, how can I use Ruby P.s which are
already created as source within irb… or is there a better way to run
ruby programs

You can use require.

Example:
require ‘path/to/song’

Michael G.

I thought you use the load command ?

Stuart

On Aug 12, 2006, at 9:32 AM, Dark A. wrote:

I thought you use the load command ?

Stuart

Either will work, but ‘load’ will reload the file even if it’s
already been loaded. ‘require’ only loads the file once.

:dudley

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