Running -r in IRB

After doing a couple of websites using Rails, I realized that my problem
is that I am terrible at ruby so I bought “Ruby by Example” by Kevin
Baird and started on page 1.

I’m at the part in chapter 2 where I am being instructed to run -r in
irb to include a required class file and I’m getting the following
error:

irb(main):001:0> -r 99bottles.rb
SyntaxError: compile error
(irb):1: syntax error, unexpected tINTEGER, expecting kDO or ‘{’ or ‘(’
-r 99bottles.rb
^
from (irb):1

When I run the file 99bottles.rb from the command prompt it doesn’t do
anything but gives me no errors. I believe that since the file creates a
new class, the book wants me to require the file so I can run new
commands against the new class in the 99bottles.rb file.

I am including the contents of the 99bottles.rb file as well…If anyone
can help point me in the right direction with including a class into irb
using -r with this file, I would be very grateful.

thanks
jackster

-------------------------99bottles.rb--------------------------
#!/usr/bin/env ruby

99 bottles problem in Ruby

class Wall

def initialize(num_of_bottles)
@bottles = num_of_bottles
end

=begin rdoc
Predicate, ends in a question mark, returns Boolean.
=end
def empty?()
@bottles.zero?
end

def sing_one_verse!()
puts sing(’ on the wall, ') + sing("\n") + take_one_down! + sing("
on the wall.\n\n")
end

private

def sing(extra=’’)
“#{(@bottles > 0) ? @bottles : ‘no more’} #{(@bottles == 1) ?
‘bottle’ : ‘bottles’} of beer” + extra
end

=begin rdoc
Destructive method named with a bang because it decrements @bottles.
Returns a String.
=end
def take_one_down!()
@bottles -= 1
'take one down, pass it around, ’
end

end

John S. wrote:

I’m at the part in chapter 2 where I am being instructed to run -r in
irb

I’m quite sure that you’re being instructed to run irb with -r and not
to
type -r into irb. I.e:
irb -rmyrubyfile

I’m quite sure that you’re being instructed to run irb with -r and not
to type -r into irb. I.e:
irb -rmyrubyfile

Or, if you are in irb, just do
load ‘myrubyfile’

or in your case
load ‘99bottles.rb’

Normally -r indicates some command line switch,i.e. in use with
ruby -r blablabla
not something you run inside irb.

Marc H. wrote:

I’m quite sure that you’re being instructed to run irb with -r and not
to type -r into irb. I.e:
irb -rmyrubyfile

Or, if you are in irb, just do
load ‘myrubyfile’

or in your case
load ‘99bottles.rb’

Normally -r indicates some command line switch,i.e. in use with
ruby -r blablabla
not something you run inside irb.

Thanks…that seemed to work. I get:

irb(main):010:0> load ‘c:\ruby4rails\99bottles.rb’
=> true

The book then tells me to create a new instance of Wall and feed it the
number 10, which I do here:

irb(main):011:0> other_wall=Wall.new(10)
=> #<Wall:0xe0371a0 @bottles=10>
irb(main):012:0>

I’m wondering why I don’t get the program to run and count the 99
bottles of beer starting with 10 in this case?

thanks for the quick replies
jackster
(I checked the errata for the book on the website and there is nothing
listed at all for this chapter)

Makes sense, Sebastian…thanks for the input.

I guess the example in the book was just suppose to be showing me how to
include a class in irb and explain some code without being able to run
the code and get the output…kind of wierd but I get the point they
are trying to make thanks to you.

I really appreciate your help with these basic questions…I’m kind of
a slow learner when it comes to code

jackster

Sebastian H. wrote:

John S. wrote:

irb(main):011:0> other_wall=Wall.new(10)
=> #<Wall:0xe0371a0 @bottles=10>
irb(main):012:0>

I’m wondering why I don’t get the program to run and count the 99
bottles of beer starting with 10 in this case?

Because nothing in your initialize method does that.
Wall.new(10) creates a new Wall object and calls its initialize method
with
the paramter 10. Since your initialize method is defined as follows:

def initialize(num_of_bottles)
@bottles = num_of_bottles
end

Wall.new(10) will return a Wall object whose @bottles instance variable
has
been set to 10. It won’t do anything more than that because it hasn’t
been
told to do anything more.

HTH,
Sebastian

John S. wrote:

irb(main):011:0> other_wall=Wall.new(10)
=> #<Wall:0xe0371a0 @bottles=10>
irb(main):012:0>

I’m wondering why I don’t get the program to run and count the 99
bottles of beer starting with 10 in this case?

Because nothing in your initialize method does that.
Wall.new(10) creates a new Wall object and calls its initialize method
with
the paramter 10. Since your initialize method is defined as follows:

def initialize(num_of_bottles)
@bottles = num_of_bottles
end

Wall.new(10) will return a Wall object whose @bottles instance variable
has
been set to 10. It won’t do anything more than that because it hasn’t
been
told to do anything more.

HTH,
Sebastian