From: James Edward Gray II Date: 2009-10-04T04:33:23+09:00 Subject: Re: Writing a Mad Libs program? On Oct 3, 2009, at 2:19 PM, John Kriple 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 Gray II