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