# \[QUIZ\] Madlibs (#28)

**URL:** <https://rubytalk.org/t/quiz-madlibs-28/17674>\
**Category:** ruby-talk\
**Created:** [15 April 2005 12:52 UTC](https://rubytalk.org/t/quiz-madlibs-28/17674 "2005-04-15T12:52:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [15 April 2005 12:52 UTC](https://rubytalk.org/t/quiz-madlibs-28/17674/1 "2005-04-15T12:52:15Z")

</div>

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/](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:
> 
> &nbsp;&nbsp;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:
> 
> &nbsp;&nbsp;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:
> 
> &nbsp;&nbsp;Our favorite language is ((gem:a gemstone)). We think ((gem)) is better  
> &nbsp;&nbsp;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:
> 
> &nbsp;&nbsp;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:
> 
> &nbsp;&nbsp;[http://rubyquiz.com/cgi-bin/madlib.cgi](http://rubyquiz.com/cgi-bin/madlib.cgi)
> 
> And here are the two Madlib files I'm using, to get you started:
> 
> &nbsp;&nbsp;[http://rubyquiz.com/madlibs/Lunch\_Hungers.madlib](http://rubyquiz.com/madlibs/Lunch_Hungers.madlib)
> 
> &nbsp;&nbsp;[http://rubyquiz.com/madlibs/Gift\_Giving.madlib](http://rubyquiz.com/madlibs/Gift_Giving.madlib)

---

<div class="post-metadata">

**Author:** ![Dominik\_Bathon](https://avatars.discourse-cdn.com/v4/letter/d/67e7ee/32.png) [@Dominik\_Bathon](https://rubytalk.org/u/Dominik_Bathon)\
**Post date:** [17 April 2005 13:18 UTC](https://rubytalk.org/t/quiz-madlibs-28/17674/2 "2005-04-17T13:18:23Z")

</div>

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)  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Give me #{str}:"  
&nbsp;&nbsp;&nbsp;&nbsp;$stdin.gets.chomp  
end

keys={}

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

> **···**
>
> On Fri, 15 Apr 2005 14:52:15 +0200, Ruby Quiz \<james@grayproductions.net\> wrote:
> 
> > 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.
> 
> =======================
> 
> And here is an even shorter version, that does the same:
> 
> keys=Hash.new { |h, k|  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "Give me #{k.sub(/\A([^:]+):/, "")}:"  
> &nbsp;&nbsp;&nbsp;&nbsp;h[$1]=$stdin.gets.chomp  
> }  
> puts "", $\*[0].split(".")[0].gsub("\_", " "), IO.read($\*[0]).gsub(/\(\(([^)]+)\)\)/) { keys[$1] }

---

<div class="post-metadata">

**Author:** ![Daniel\_Amelang](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Daniel\_Amelang](https://rubytalk.org/u/Daniel_Amelang)\
**Post date:** [18 April 2005 06:00 UTC](https://rubytalk.org/t/quiz-madlibs-28/17674/3 "2005-04-18T06:00:53Z")

</div>

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

Dan

---

<div class="post-metadata">

**Author:** ![Carl\_Youngblood5](https://avatars.discourse-cdn.com/v4/letter/c/4491bb/32.png) [@Carl\_Youngblood5](https://rubytalk.org/u/Carl_Youngblood5)\
**Post date:** [18 April 2005 18:54 UTC](https://rubytalk.org/t/quiz-madlibs-28/17674/4 "2005-04-18T18:54:14Z")

</div>

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.

> **···**
>
> On 4/17/05, Daniel Amelang \<daniel.amelang@gmail.com\> wrote:
> 
> > Has anyone else got the strange feeling that \_why's poignant guide is  
> > one really big madlib?
> > 
> > Dan
