Writing a Mad Libs program?

B

Britt Hayes

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

Any examples would be greatly appreciated.

Thanks.
 
J

John Kriple

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?
 
J

James Edward Gray 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?

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 Gray II
 
J

John Kriple

$ 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 Gray II
 
J

James Edward Gray II

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.
Where does this get written?

This we me interacting with the program as it ran. It asked those
questions and I typed those answers.
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
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top