[QUIZ] Madlibs (#28)

R

Ruby Quiz

The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rubyquiz.com/

3. Enjoy!

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

This week's Ruby Quiz is to write a program that presents the user with Madlibs.
The script should ask the user for a series of words, then fill in the proper
places in the story using the user's answers.

We'll keep our story format very simple, using a ((...)) notation for
placeholders. Here's an example:

Our favorite language is ((a gemstone)).

If your program is fed that template, it should ask you to enter "a gemstone"
and then display your version of the story:

Our favorite language is Ruby.

That covers the simple cases, but in some instances we may want to reuse an
answer. For that, we'll introduce a way to name them:

Our favorite language is ((gem:a gemstone)). We think ((gem)) is better
than ((a gemstone)).

With the above story, your program should ask for two gemstones, then substitute
the one designated by ((gem:...)) at ((gem)). That would give results like:

Our favorite language is Ruby. We think Ruby is better than Emerald.

You can choose any interface you like, as long as person can interact with the
end result. You can play around with my solution here:

http://rubyquiz.com/cgi-bin/madlib.cgi

And here are the two Madlib files I'm using, to get you started:

http://rubyquiz.com/madlibs/Lunch_Hungers.madlib

http://rubyquiz.com/madlibs/Gift_Giving.madlib
 
D

Dominik Bathon

This week's Ruby Quiz is to write a program that presents the user with
Madlibs.
The script should ask the user for a series of words, then fill in the
proper
places in the story using the user's answers.

Here is my solution. It is a very simplistic command line interface
version, but it does the job :)
Just give it the madlib filename as first argument.

The Code:

def ask_for(str)
puts "Give me #{str}:"
$stdin.gets.chomp
end

keys={}

puts "", ARGV[0].split(".")[0].gsub("_", " "),
IO.read(ARGV[0]).gsub(/\(\(([^)]+)\)\)/) {
if (t=$1) =~ /\A([^:]+):(.+)\z/
keys[$1]=ask_for($2)
else
keys[t] || ask_for(t)
end
}

=======================

And here is an even shorter version, that does the same:

keys=Hash.new { |h, k|
puts "Give me #{k.sub(/\A([^:]+):/, "")}:"
h[$1]=$stdin.gets.chomp
}
puts "", $*[0].split(".")[0].gsub("_", " "),
IO.read($*[0]).gsub(/\(\(([^)]+)\)\)/) { keys[$1] }
 
D

Daniel Amelang

Has anyone else got the strange feeling that _why's poignant guide is
one really big madlib?

Dan
 
C

Carl Youngblood

I got a good laugh from your suggestion, but I would submit that
_why's singular variety of dementia could only be the result of a more
deliberate effort.
 

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

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top