Skip to content

Instantly share code, notes, and snippets.

@illbzo1
Created November 8, 2011 03:15
Show Gist options
  • Save illbzo1/1346908 to your computer and use it in GitHub Desktop.
Save illbzo1/1346908 to your computer and use it in GitHub Desktop.
Madlibs Generator, Objectified
@counter = 0
files = Dir.glob("*.madlib")
if files.size > 1
puts "CHOOSE YOUR FILE:"
x = 0
files.each do |file|
x += 1
puts "#{x}. #{file}"
end
filename = files[gets.chomp.to_i - 1]
elsif files.size == 1
filename = files[0]
else
puts "No files found."
exit
end
madlib_text = IO.read(filename)
parts_of_speech = %w(NOUN ADJECTIVE VERB)
parts_of_speech.each do |pos|
word_matches = madlib_text.scan(/(#{pos}(\d+)?)/).map { |v| v[0] }
unique_numbered_words = (word_matches - [pos]).uniq
word_size = 0
word_size += unique_numbered_words.size
word_size += word_matches.select { |v| v == pos }.size
unless word_size == 0
puts "We're gonna need #{word_size} #{pos.downcase}s."
@words = []
word_size.times { @words << gets.chomp }
unique_numbered_words.each do |word_text|
madlib_text = madlib_text.gsub(word_text, @words.shift)
end
madlib_text = madlib_text.gsub(pos) do |v|
@words.shift
end
end
end
puts madlib_text
Look at this NOUN
Isn't it neat?
Wouldn't you think my NOUN's complete?
Wouldn't you think I'm the NOUN1
The NOUN1 who has ev'rything?
Look at this trove
NOUNs untold
How many NOUNs can one NOUN hold?
Lookin' around here you'd think
(Sure) she's got everything
I've got NOUNs and NOUNs aplenty
I've got NOUNs and NOUNs galore
(You want NOUNs?
I got twenty)
But who cares?
No ADJECTIVE deal
I want more
I wanna be where the NOUNs are
I wanna see
Wanna see 'em dancin'
Walkin' around on those
(Whad'ya call 'em?) oh - feet
Flippin' your fins you don't get too far
Legs are required for jumpin', dancin'
Strollin' along down a
(What's that word again?) street
Up where they walk
Up where they run
Up where they stay all day in the sun
Wanderin' free
Wish I could be
Part of that world
What would I give
If I could live
Outta these waters?
What would I pay
To spend a day
ADJECTIVE on the sand?
Betcha on land
They understand
Bet they don't VERB their daughters
ADJECTIVE ADJECTIVE women
Sick o' swimmin'
Ready to stand
And ready to VERB what the people know
VERB 'em my NOUNs
And get some answers
What's a NOUN and why does it
(What's the word?) burn?
When's it my turn?
Wouldn't I love
Love to explore that NOUN above?
Out of the sea
Wish I could be
Part of that world
@jqr
Copy link

jqr commented Nov 8, 2011

Sup with that @counter on line 1? Looks unused.

@illbzo1
Copy link
Author

illbzo1 commented Nov 8, 2011

@jqr whoops, I think that's a holdover from an early version of the code that was replaced by x later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment