# Writing a Mad Libs program?

**URL:** <https://rubytalk.org/t/writing-a-mad-libs-program/55648>\
**Category:** ruby-talk\
**Created:** [3 October 2009 14:39 UTC](https://rubytalk.org/t/writing-a-mad-libs-program/55648 "2009-10-03T14:39:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Britt\_Hayes](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@Britt\_Hayes](https://rubytalk.org/u/Britt_Hayes)\
**Post date:** [3 October 2009 14:39 UTC](https://rubytalk.org/t/writing-a-mad-libs-program/55648/1 "2009-10-03T14:39:06Z")

</div>

How would I go about writing a 'Mad Libs' type program using Ruby?

Any examples would be greatly appreciated.

Thanks.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![JEG2](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@JEG2](https://rubytalk.org/u/JEG2)\
**Post date:** [3 October 2009 14:44 UTC](https://rubytalk.org/t/writing-a-mad-libs-program/55648/2 "2009-10-03T14:44:24Z")

</div>

Have a look at the solutions to this old Ruby Quiz:

&nbsp;&nbsp;&nbsp;[Ruby Quiz - Madlibs (#28)](http://www.rubyquiz.com/quiz28.html)

James Edward Gray II

> **···**
>
> On Oct 3, 2009, at 9:39 AM, Britt Hayes wrote:
> 
> > How would I go about writing a 'Mad Libs' type program using Ruby?
> > 
> > Any examples would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![John\_Kriple](https://avatars.discourse-cdn.com/v4/letter/j/ba8739/32.png) [@John\_Kriple](https://rubytalk.org/u/John_Kriple)\
**Post date:** [3 October 2009 19:19 UTC](https://rubytalk.org/t/writing-a-mad-libs-program/55648/3 "2009-10-03T19:19:01Z")

</div>

I had looked at those before, but I couldn't follow very well as I am  
very new to Ruby.

Would you be able to get me started using one of those examples?

How does the mad lib script I wrote work with the commands?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![JEG2](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@JEG2](https://rubytalk.org/u/JEG2)\
**Post date:** [3 October 2009 19:33 UTC](https://rubytalk.org/t/writing-a-mad-libs-program/55648/4 "2009-10-03T19:33:23Z")

</div>

Here's an example of me using the code from the first solution to play with the example in the quiz:

$ ruby madlib.rb madlib.txt  
Give me a gemstone:  
Ruby  
Give me a gemstone:  
Emerald

madlib  
Our favorite language is Ruby. We think Ruby is better  
than Emerald.  
$ cat madlib.rb  
def ask\_for(str)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Give me #{str}:"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$stdin.gets.chomp  
end

keys={}

puts "", ARGV[0].split(".")[0].gsub("\_", " "),  
IO.read(ARGV[0]).gsub(/\(\(([^)]+)\)\)/) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (t=$1) =~ /\A([^:]+):(.+)\z/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keys[$1]=ask\_for($2)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keys[t] || ask\_for(t)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
}  
$ cat madlib.txt  
Our favorite language is ((gem:a gemstone)). We think ((gem)) is better  
than ((a gemstone)).

Hope that helps.

James Edward Gray II

> **···**
>
> On Oct 3, 2009, at 2:19 PM, John Kriple wrote:
> 
> > I had looked at those before, but I couldn't follow very well as I am  
> > very new to Ruby.
> > 
> > Would you be able to get me started using one of those examples?
> > 
> > How does the mad lib script I wrote work with the commands?

---

<div class="post-metadata">

**Author:** ![John\_Kriple](https://avatars.discourse-cdn.com/v4/letter/j/ba8739/32.png) [@John\_Kriple](https://rubytalk.org/u/John_Kriple)\
**Post date:** [3 October 2009 20:47 UTC](https://rubytalk.org/t/writing-a-mad-libs-program/55648/5 "2009-10-03T20:47:13Z")

</div>

> $ ruby madlib.rb madlib.txt

I am using nano for a script shell. Does that have an effect on this?

> Give me a gemstone:  
> Ruby  
> Give me a gemstone:  
> Emerald

Where does this get written?

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

So, I guess my biggest question is where does the script get written  
before you start writing the program to fill in the gaps?

> **···**
>
> > $ cat madlib.rb  
> > def ask\_for(str)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Give me #{str}:"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$stdin.gets.chomp  
> > end
> > 
> > keys={}
> > 
> > puts "", ARGV[0].split(".")[0].gsub("\_", " "),  
> > IO.read(ARGV[0]).gsub(/\(\(([^)]+)\)\)/) {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (t=$1) =~ /\A([^:]+):(.+)\z/  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keys[$1]=ask\_for($2)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keys[t] || ask\_for(t)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > }  
> > $ cat madlib.txt  
> > Our favorite language is ((gem:a gemstone)). We think ((gem)) is better  
> > than ((a gemstone)).
> > 
> > Hope that helps.
> > 
> > James Edward Gray II
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![JEG2](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@JEG2](https://rubytalk.org/u/JEG2)\
**Post date:** [3 October 2009 21:55 UTC](https://rubytalk.org/t/writing-a-mad-libs-program/55648/6 "2009-10-03T21:55:38Z")

</div>

> > $ ruby madlib.rb madlib.txt
> 
> I am using nano for a script shell. Does that have an effect on this?

I believe you mean that you are using nano for your text editor, not shell. Most likely your shell is bash, if you are on a Unix and haven't tried to change it. The commands I showed, lines starting with $, will work fine in bash.

> > Give me a gemstone:  
> > Ruby  
> > Give me a gemstone:  
> > Emerald
> 
> Where does this get written?

This we me interacting with the program as it ran. It asked those questions and I typed those answers.

> > madlib  
> > Our favorite language is Ruby. We think Ruby is better  
> > than Emerald.
> 
> So, I guess my biggest question is where does the script get written  
> before you start writing the program to fill in the gaps?

Using nano, I can create the file like this:

1. I type: nano.rb  
2. I paste in the code from the web site or my last message  
3. I push control-o and then press return to write the file

Does that help?

James Edward Gray II

> **···**
>
> On Oct 3, 2009, at 3:47 PM, John Kriple wrote:
