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

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

···

from :0

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

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

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

:dudley

···

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

I thought you use the load command ?

Stuart