My Mad Libs Ruby App
This post is really just so I can say how proud of myself I am. I just finished cleaning up my most advanced app to date, and felt like sharing.
A few days ago, I randomly got the idea to make a Mad Libs Ruby app. I knew that the code would involve a lot of repetition, which would involve the use of methods, but—while I had read about them—I hadn't actually learned how to use them yet. I instead decided to just write it with horribly inefficient code, and then clean it up. It would also serve to illustrate just how much code the final version would save. Here is the original version:
# Mad Lib
# madlib.rbverb = []
adj = []
noun = []
adv = []
puts 'Type in three verbs, followed by return after each one:'
input = gets.chomp.upcase
verb.push input
input = gets.chomp.upcase
verb.push input
input = gets.chomp.upcase
verb.push input
puts 'Type in three adjectives, followed by return after each one:'
input = gets.chomp.upcase
adj.push input
input = gets.chomp.upcase
adj.push input
input = gets.chomp.upcase
adj.push input
puts 'Type in three nouns, followed by return after each one:'
input = gets.chomp.upcase
noun.push input
input = gets.chomp.upcase
noun.push input
input = gets.chomp.upcase
noun.push input
puts 'Type in three adverbs, followed by return after each one:'
input = gets.chomp.upcase
adv.push input
input = gets.chomp.upcase
adv.push input
input = gets.chomp.upcase
adv.push input
puts 'Thanks! Now I\'d like to tell you a story.'
puts 'One day a very ' + adj[0] + ' ' + noun[0] + ' went walking'
puts 'down the street. It came upon a ' + noun[1] + ' running ' + adv[0]
puts 'after a stranger sight. The ' + noun[0] + ' ' + verb[0] + 'ed a ' + noun[2]
puts 'with a ' + adj[1] + ' ' + noun[0] + '. But that\'s OK'
puts 'because he remembered to ' + verb[1] +'.'
It starts out by creating four empty arrays, for each part of speech. Then it gets ugly. The core part of the code is this:
input = gets.chomp.upcase
verb.push input
It creates the variable "input," then gets it from the user (and also chomps off the return and makes it into all caps, so that they stand out in the final story). It then pushes the input into the array for that part of speech (in this case, "verb"). The problem is that these two lines get repeated exactly the same two more times for each part of speech, and the only difference in the code for each part of speech is a single word, defining which array it goes into. That's twelve repetitions of two lines of code. Here is the new version:
def ask part
3.times do
input = gets.chomp.upcase
part.push input
end
end
verb = []
adj = []
noun = []
adv = []
puts 'Type in three verbs, followed by return after each one:'
ask verb
puts 'Type in three adjectives, followed by return after each one:'
ask adj
puts 'Type in three nouns, followed by return after each one:'
ask noun
puts 'Type in three adverbs, followed by return after each one:'
ask adv
puts 'Thanks! Now I\'d like to tell you a story.'
[The story part is the same as in the original, so I didn't repeat it here]
As you can see, I defined a new method, "ask." It gets the variable "part" (for "part of speech"). Since I ask each question twice, I added that to the definition. The core part of the code is the same as before, except that I use the "part" variable instead. Then for each part of speech, all I need to do is ask, and pass it which part of speech. For each question, six lines becomes two words, and all I added were six lines (two of which are just "end").
Of course, there's also the story itself. It's mostly just a placeholder right now. It doesn't even use all of the words the user inputs. I'll have to work on it. Also, there's one part where I just add an "-ed" on the end of a verb, but that doesn't work out very well for a lot of verbs. Of course, it's kind of amusing to see "RUNed," so maybe I'll keep it.
Here it is when I run it in Terminal:
kirk$ Ruby madlib.rb
Type in three verbs, followed by return after each one:
beat
steal
worship
Type in three adjectives, followed by return after each one:
blue
smelly
smooth
Type in three nouns, followed by return after each one:
desk
floor
shoe
Type in three adverbs, followed by return after each one:
silently
violently
nonchalantly
Thanks! Now I'd like to tell you a story.
One day a very BLUE DESK went walking
down the street. It came upon a FLOOR running SILENTLY
after a stranger sight. The DESK BEATed a SHOE
with a SMELLY DESK. But that's OK
because he remembered to STEAL.
0 TrackBacks
Listed below are links to blogs that reference this entry: My Mad Libs Ruby App.
TrackBack URL for this entry: http://kirk.luceo.net/mt/mt-tb.cgi/86

Leave a comment