[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

IRB and rb files

jctown@nb.sympatico.ca

8/12/2006 1:58:00 PM

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

2 Answers

Dark Ambient

8/12/2006 2:33:00 PM

0

I thought you use the load command ?

Stuart

On 8/12/06, Michael Guterl <mguterl@gmail.com> wrote:
> Hello,
>
> On 8/12/06, ClassRubyExceptionHandline <jctown@nb.sympatico.ca> wrote:
> >
> > 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
> >
> >
> >
> You can use require.
>
> Example:
> require 'path/to/song'
>
> Michael Guterl
>
>

Dudley Flanders

8/12/2006 2:43:00 PM

0


On Aug 12, 2006, at 9:32 AM, Dark Ambient 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