# Newbie Question On Ruby Quiz

**URL:** <https://www.ruby-forum.com/t/newbie-question-on-ruby-quiz/141717>\
**Category:** Ruby\
**Created:** [June 30, 2008, 1:08am UTC](https://www.ruby-forum.com/t/newbie-question-on-ruby-quiz/141717 "2008-06-30T01:08:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kylemathews](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/51bf81/32.png) [@kylemathews](https://www.ruby-forum.com/u/kylemathews)\
**Post date:** [June 30, 2008, 1:08am UTC](https://www.ruby-forum.com/t/newbie-question-on-ruby-quiz/141717/1 "2008-06-30T01:08:14Z")

</div>

I’m a programming and Ruby newbie. I wanted to build some programs, so  
I started Best of Ruby Q…

The first quiz is MadLibs ([Ruby Quiz - Madlibs (#28)](http://www.rubyquiz.com/quiz28.html)) and,  
given the answer, I can’t even get it to work.

I have a ruby file with the answer in it:

# use Ruby’s standard template engine

require “erb”

# storage for keyed question reuse

$answers = Hash.new

# asks a madlib question and returns an answer

def q\_to\_a( question )  
question.gsub!(/\s+/, " ") # normalize spacing

if $answers.include? question # keyed question  
$answers[question]  
else # new question  
key = if question.sub!(/^\s\*(.+?)\s\*:\s\*/, “”) then $1 else nil end

```
print "Give me #{question}: "
answer = $stdin.gets.chomp

$answers[key] = answer unless key.nil?

answer

```

end  
end

# usage

unless ARGV.size == 1 and test(?e, ARGV[0])  
puts “Usage: #{File.basename($PROGRAM\_NAME)} MADLIB\_FILE”  
exit  
end

# load Madlib, with title

madlib = “\n#{File.basename(ARGV.first, ‘.madlib’).tr(‘\_’, ’ ')}\n\n” +  
File.read(ARGV.first)

# convert ((…)) to \<%= q\_to\_a(‘…’) %\>

madlib.gsub!(/((\s\*(.+?)\s\*))/, “\<%= q\_to\_a(‘\1’) %\>”)

# run template

ERB.new(madlib).run

And a .madlib file with (copied from the book):  
Our favorite language is ((gem:a gemstone)). We think ((gem)) is  
better than ((a gemstone)).

Whenever I run the .rb file I get this error: Usage: madlib.rb  
MADLIB\_FILE

My question is basically: how do I make this program work? Thank you.

---

<div class="post-metadata">

**Author:** ![kylemathews](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/51bf81/32.png) [@kylemathews](https://www.ruby-forum.com/u/kylemathews)\
**Post date:** [June 30, 2008, 1:58am UTC](https://www.ruby-forum.com/t/newbie-question-on-ruby-quiz/141717/2 "2008-06-30T01:58:52Z")

</div>

All,

this is a very basic question about Variables and I wish to clear it up.  
I  
have not yet successfully mastered Object programming and still getting  
my  
head around the whole story.

Global Variables are accessible anywhere within the program and are not  
recommended style of programming in OOPS.

Local Variables are isolated to the Object or method.

Instant Variables I am completely mystified on their purpose and their  
difference between a local variable.

Sean

---

<div class="post-metadata">

**Author:** ![kylemathews](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/51bf81/32.png) [@kylemathews](https://www.ruby-forum.com/u/kylemathews)\
**Post date:** [June 30, 2008, 2:01am UTC](https://www.ruby-forum.com/t/newbie-question-on-ruby-quiz/141717/3 "2008-06-30T02:01:07Z")

</div>

Kyle M. wrote:

> I’m a programming and Ruby newbie. I wanted to build some programs, so  
> I started Best of Ruby Q…
> 
> The first quiz is MadLibs ([Ruby Quiz - Madlibs (#28)](http://www.rubyquiz.com/quiz28.html)) and,  
> given the answer, I can’t even get it to work.

(…)

> # usage
> 
> unless ARGV.size == 1 and test(?e, ARGV[0])  
> puts “Usage: #{File.basename($PROGRAM\_NAME)} MADLIB\_FILE”  
> exit  
> end  
> (…)
> 
> And a .madlib file with (copied from the book):  
> Our favorite language is ((gem:a gemstone)). We think ((gem)) is  
> better than ((a gemstone)).
> 
> Whenever I run the .rb file I get this error: Usage: madlib.rb  
> MADLIB\_FILE
> 
> My question is basically: how do I make this program work? Thank you.

It’s trying to tell you that you’re supossed to run madlib.rb with an  
argument, specifying which .madlib file to use.  
So don’t start it with:

madlib.rb

use  
madlib.rb whatever\_you\_named\_it.madlib

instead. Hth,

Siep

---

<div class="post-metadata">

**Author:** ![kylemathews](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/51bf81/32.png) [@kylemathews](https://www.ruby-forum.com/u/kylemathews)\
**Post date:** [June 30, 2008, 10:45am UTC](https://www.ruby-forum.com/t/newbie-question-on-ruby-quiz/141717/4 "2008-06-30T10:45:24Z")

</div>

Sean Murphy wrote:

> Instant Variables I am completely mystified on their purpose and their  
> difference between a local variable.

They’re called instance variables, not instant variables. And they’re  
called  
that because they’re specific to an instance of a class (i.e. to an  
object).  
For example take the following class:  
class Foo  
def initialize(bar)  
@bar = bar  
end  
def bar  
@bar  
end  
end  
f1 = Foo.new(5)  
f2 = Foo.new(4)  
f1.bar #=\> 5  
f2.bar #=\> 4

So here you have two instances of the class Foo. One instance has the  
value 5  
stored in the instance variable @bar, the other one the value 4. As long  
as  
f1 and f2 exist you will be able to access their values for @bar via the  
bar-  
method. If @bar were a local variable, it would disappear after the  
initialize  
method is done and thus would not be accessible from the bar method. If  
@bar  
was a global variable, it would not be object-specific, so f1.bar and  
f2.bar  
would return the same thing.

HTH,  
Sebastian
