Skip to content
Browse files

Command-line arguments!

  • Loading branch information...
1 parent cc19eb5 commit 3c3fbbde4c9fc7ff11e7cfbed677b49b4cd6cc8f @cowmanifestation committed Mar 2, 2011
Showing with 11 additions and 13 deletions.
  1. +11 −13 mad_libs.rb
View
24 mad_libs.rb
@@ -1,6 +1,6 @@
class MadLibs
def initialize(filename)
- @file = filename
+ @file = File.new(filename)
run
end
@@ -11,7 +11,7 @@ def get_prompts(sentence)
def get_substitution(prompt)
print prompt + ": "
- substitution = gets.chomp
+ substitution = STDIN.gets.chomp
return substitution
end
@@ -36,7 +36,7 @@ def replace(sentence, pattern, substitution)
sentence.gsub!(/\(\(#{pattern}\)\)/, substitution)
end
- def ask_if_continuing(file)
+ def ask_if_continuing
print "Continue? (y/n) "
answer = gets
if answer == "n\n"
@@ -46,21 +46,19 @@ def ask_if_continuing(file)
def run
- File.open(@file) do |file|
- file.each do |line|
- keywords = []
- prompts = get_prompts(line)
+ @file.each do |line|
+ keywords = []
+ prompts = get_prompts(line)
- substitute(line, keywords, prompts)
+ substitute(line, keywords, prompts)
- puts line
+ puts line
- unless file.eof?
- ask_if_continuing(file)
- end
+ unless @file.eof?
+ ask_if_continuing
end
end
end
end
-m = MadLibs.new("sentences.txt")
+m = MadLibs.new(ARGV[0])

0 comments on commit 3c3fbbd

Please sign in to comment.
Something went wrong with that request. Please try again.