Skip to content

Instantly share code, notes, and snippets.

@coryschires
Created April 13, 2012 01:33
Show Gist options
  • Save coryschires/2372674 to your computer and use it in GitHub Desktop.
Save coryschires/2372674 to your computer and use it in GitHub Desktop.
99 bottles
# 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"
@tjbladez
Copy link

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

@coryschires
Copy link
Author

Ha. Was posting this to help a friend who's learning. How have you been? Let's hang out.

@tjbladez
Copy link

Totally. Let me know when

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment