Skip to content

Instantly share code, notes, and snippets.

@treble37
Created May 16, 2013 05:40
Show Gist options
  • Save treble37/5589616 to your computer and use it in GitHub Desktop.
Save treble37/5589616 to your computer and use it in GitHub Desktop.
Playing with mad libs in ruby....
sentence_library=["I had a ((an adjective)) sandwich for lunch today. It dripped all over my ((a body part)) and ((a noun)).",
"Our favorite language is ((gem:a gemstone)). We think ((gem)) is better than ((a gemstone))."]
#token_arr = sentence.scan(/\(\(([^)]+)\)\)/)
sentence = sentence_library[rand(sentence_library.size)]
token_arr = sentence.scan(/\(\(.*?\)\)/)
hsh = {}
token_arr.each do |t|
puts "Enter a word for #{t.delete('()')}?"
hsh[t] = gets.chomp
end
hsh.each do |key,val|
sentence.gsub!(key,val)
end
p sentence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment