Newbie Question On Ruby Quiz

K

Kyle Murphy

I'm a programming and Ruby newbie. I wanted to build some programs, so
I started Best of Ruby Quiz.

The first quiz is MadLibs (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.
 
S

Sean Murphy

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
 
S

Siep Korteling

Kyle said:
I'm a programming and Ruby newbie. I wanted to build some programs, so
I started Best of Ruby Quiz.

The first quiz is MadLibs (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
 
S

Sebastian Hungerecker

Sean said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top