Writing a Mad Libs program?

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

Any examples would be greatly appreciated.

Thanks.

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.

Have a look at the solutions to this old Ruby Q.:

http://www.rubyquiz.com/quiz28.html

James Edward G. II

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?

On Oct 3, 2009, at 2:19 PM, John K. 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?

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)
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
}
$ cat madlib.txt
Our favorite language is ((gem:a gemstone)). We think ((gem)) is better
than ((a gemstone)).

Hope that helps.

James Edward G. II

On Oct 3, 2009, at 3:47 PM, John K. wrote:

$ 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 G. II

$ 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)
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
}
$ cat madlib.txt
Our favorite language is ((gem:a gemstone)). We think ((gem)) is better
than ((a gemstone)).

Hope that helps.

James Edward G. II

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs