Created
April 13, 2012 01:33
-
-
Save coryschires/2372674 to your computer and use it in GitHub Desktop.
99 bottles
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
# Iterative solution | |
# | |
bottles = (0..99).to_a | |
bottles.reverse.each do |bottle| | |
if bottle.zero? | |
puts "#{bottle} bottles of beer on the wall. No more beer" | |
else | |
puts "#{bottle} bottles of beer on the wall.." | |
end | |
end | |
# Looping solution | |
# | |
bottles = 99 | |
until bottles.zero? # you could also write: while bottles != 0 | |
puts "#{bottles} bottles of beer on the wall.." | |
bottles = bottles - 1 # you could also use a shorthand: bottles -= 1 | |
end | |
puts "#{bottles} bottles of beer on the wall. No more beer" |
Ha. Was posting this to help a friend who's learning. How have you been? Let's hang out.
Totally. Let me know when
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Real solution...
bottles = (0..99).to_a
cory = User.find_by_slug('coryschires')
bottles.reverse.inject(0) do |acc, bottle|
if acc > 7
puts "Some amount of bottles of beer on the wall. Cory is drunk so who cares..."
next
end
if bottle > 1
cory.drink!
acc +=1
puts "#{bottle} bottles of beer on the wall... Cory took one..."
else
puts "No mor beer on the wall. Please supply more alcohol.."
end
end