Q1 of BORQ - Mad Libs
My solution for Mad Libs, I need to practice some more regular expression. Rubular is a great help for people like me. RegExp is in a more free-form in Ruby compared to python, which is quite good for quick experiments on irb.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# solution to simpler part | |
# puts gets.chop.gsub!( /\(\([^))]*\)\)/) { |w| w=gets.chop} | |
# part 2 - along with substitution | |
dict = {} | |
puts IO.read(ARGV.shift).chop.gsub!( /\(\([^))]*\)\)/) { |w| | |
w = w.scan(/[^()]+/)[0] | |
combo = w.split(":") | |
if (combo.size==2) | |
print "Enter #{combo[1]} : " | |
w = dict[combo[0]] = gets.chop | |
else | |
if (dict[combo[0]] == nil) | |
print "Enter #{combo[0]} : " | |
w=gets.chop | |
else | |
w = dict[combo[0]] | |
end | |
end | |
} |
I wish I could even get rid of the "if"s and make it sexier. Can't think more on this one right now.
Labels: Ruby