# Running -r in IRB

**URL:** <https://www.ruby-forum.com/t/running-r-in-irb/121906>\
**Category:** Ruby\
**Created:** [December 2, 2007, 6:56pm UTC](https://www.ruby-forum.com/t/running-r-in-irb/121906 "2007-12-02T18:56:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [December 2, 2007, 6:56pm UTC](https://www.ruby-forum.com/t/running-r-in-irb/121906/1 "2007-12-02T18:56:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [December 2, 2007, 7:03pm UTC](https://www.ruby-forum.com/t/running-r-in-irb/121906/2 "2007-12-02T19:03:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [December 2, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/running-r-in-irb/121906/3 "2007-12-02T19:15:00Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [December 2, 2007, 7:26pm UTC](https://www.ruby-forum.com/t/running-r-in-irb/121906/4 "2007-12-02T19:26:27Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [December 2, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/running-r-in-irb/121906/5 "2007-12-02T20:02:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [December 2, 2007, 7:38pm UTC](https://www.ruby-forum.com/t/running-r-in-irb/121906/6 "2007-12-02T19:38:59Z")

</div>

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
