IRB and rb files

  • Thread starter ClassRubyExceptionHandline
  • Start date
C

ClassRubyExceptionHandline

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 Programs which are
already created as source within irb... or is there a better way to run
ruby programs
 
D

Dudley Flanders

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,052
Members
48,769
Latest member
Clifft

Latest Threads

Top