Class: MadLib
- Inherits:
-
Object
- Object
- MadLib
- Defined in:
- lib/madlib_lynn_gem.rb
Overview
MadLib class
- Author
-
Lynn Conway
MadLib class implements the MadLib class. The user chooses the type of MadLib to play and then enters the different types of words prompted for. When all words have been entered, the Mad Lib text is printed out using the words prompted for.
Instance Method Summary collapse
-
#collect_word_types ⇒ Object
of hashes, @word_array, as the keys.
-
#get_madlib_type ⇒ Object
the string in the variable @madlib.
-
#initialize ⇒ MadLib
constructor
enters.
-
#play ⇒ Object
Method to play the MadLib game.
-
#print_madlib ⇒ Object
Print out @story string.
- #read_madlib ⇒ Object
-
#user_input ⇒ Object
of hashes, @word_array, as the values.
Constructor Details
#initialize ⇒ MadLib
enters. Initialize to empty array.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/madlib_lynn_gem.rb', line 22 def initialize @story = "" @word_array = [] @madlib_types = ["LooneyTune", "Space", "Penguin"] puts "Which MadLib would you like to play?" n = @madlib_types.length for i in 0...(n-1) print @madlib_types[i] + " or " end print @madlib_types[n-1] + "? " end |
Instance Method Details
#collect_word_types ⇒ Object
of hashes, @word_array, as the keys.
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/madlib_lynn_gem.rb', line 55 def collect_word_types @story_array.each do |line| this_line = line.split this_line.each do |word| if word =~ /@@@/ word.sub!(/@@@/, "").sub!(/\W/, "") @word_array.push({word => ""}) end end end end |
#get_madlib_type ⇒ Object
the string in the variable @madlib.
37 38 39 |
# File 'lib/madlib_lynn_gem.rb', line 37 def get_madlib_type @madlib = gets.chomp end |
#play ⇒ Object
Method to play the MadLib game.
98 99 100 101 102 103 104 |
# File 'lib/madlib_lynn_gem.rb', line 98 def play self.get_madlib_type self.read_madlib self.collect_word_types self.user_input self.print_madlib end |
#print_madlib ⇒ Object
Print out @story string.
86 87 88 89 90 91 92 93 94 |
# File 'lib/madlib_lynn_gem.rb', line 86 def print_madlib @word_array.each do |h| h.each do |key, value| @word = "@@@" << key @story.sub!(/#{Regexp.quote(@word)}/, value) end end puts @story end |
#read_madlib ⇒ Object
44 45 46 47 48 49 |
# File 'lib/madlib_lynn_gem.rb', line 44 def read_madlib @story_array = IO.readlines("#{@madlib}_madlib.txt") @story_array.each do |line| @story += line end end |
#user_input ⇒ Object
of hashes, @word_array, as the values.
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/madlib_lynn_gem.rb', line 71 def user_input @word_array.each do |h| h.each_key do |key| print "Enter a #{key}: " v = gets.chomp h[key] = v end end end |