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