Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Chapter 7 ##
- ### 99 Bottles of Beer on the Wall
- SONG_BOTTLES = 99
- print "Lyrics of the song #{SONG_BOTTLES} Bottles of Beer\n\n"
- remaining_bottles = SONG_BOTTLES
- while remaining_bottles > 0
- print "#{remaining_bottles} bottles of beer on the wall, " <<
- "#{remaining_bottles} bottles of beer.\n" <<
- "Take one down and pass it around, " <<
- "#{remaining_bottles -= 1} bottles of beer on the wall.\n\n"
- end
- print "No more bottles of beer on the wall, no more bottles of beer.\n" <<
- "Go to the store and buy some more, #{SONG_BOTTLES} bottles of beer on the wall.\n"
- ### Deaf grandma
- MIN_YEAR = 1930
- MAX_YEAR = 1950
- print "Hello kid, what do you want from grandma? "
- request = gets.chomp
- while request.index("BYE").nil?
- if request != request.upcase
- print "HUH?! SPEAK UP, SONNY! "
- else
- year = rand((MAX_YEAR - MIN_YEAR) + 1) + MIN_YEAR
- print "NO, NOT SINCE #{year}! "
- end
- request = gets.chomp
- end
- ### Deaf grandma extended
- MIN_YEAR = 1930
- MAX_YEAR = 1950
- BYE_MAX_TRIES = 3
- print "Hello kid, what do you want from grandma? "
- bye_tries_in_a_row = 0
- while bye_tries_in_a_row < BYE_MAX_TRIES
- request = gets.chomp
- if request.index("BYE").nil?
- bye_tries_in_a_row = 0
- if request != request.upcase
- print "HUH?! SPEAK UP, SONNY! "
- else
- year = rand((MAX_YEAR - MIN_YEAR) + 1) + MIN_YEAR
- print "NO, NOT SINCE #{year}! "
- end
- else
- bye_tries_in_a_row += 1
- if bye_tries_in_a_row == BYE_MAX_TRIES
- break
- end
- print "I CAN'T HEAR YOU! "
- end
- end
- ### Leap years
- print "Hello there! We are going to get the leap years in a range, OK?\n"
- print "Starting year: "
- starting_year = gets.to_i
- print "Ending year: "
- ending_year = gets.to_i
- leap_years = []
- current_year = starting_year
- while current_year <= ending_year
- if current_year.remainder(4) == 0 &&
- (current_year.remainder(100) != 0 || current_year.remainder(400) == 0)
- leap_years.push current_year
- end
- current_year += 1
- end
- if leap_years.length > 0
- puts "The leap years between #{starting_year} and #{ending_year} are:"
- puts leap_years
- else
- puts "There are no leap years between #{starting_year} and #{ending_year}."
- end
Add Comment
Please, Sign In to add comment