Chris Pine Tutorial 99 Bottles of Beer Program

On Thu, Mar 28, 2013 at 9:33 AM, Phil H. [email protected] wrote:

seeking out help online.


Posted via http://www.ruby-forum.com/.

Okay, but we don’t know what the book has or hasn’t taught, so how can
we
know not to say to use downto? Besides, even if it hasn’t been covered,
I
don’t think it’s too much to go look the method up in the docs and
understand it.

-Josh

Though I wish 1 beer minus 100 beers always left 100 beers remaining
some people’s code above never goes below 99 (creating an infinite
loop). I made mine only using what we have learned from the book so far.

It would have been easier to figure out the answer if Chris had
explained that when you set a value of a variable outside a loop it can
later get modified while inside the loop. Also, if you are newbie like
myself, it might not be immediately obvious why I have a second
variable: “bottles_of_beer2”. It was so I could have a count of the
beers for the the third line of the song that is based on the already
lower count of beers in the 1st and 2nd lines of the song.

bottles_of_beer = 100
while bottles_of_beer > 1
bottles_of_beer = bottles_of_beer - 1
bottles_of_beer2 = bottles_of_beer - 1
puts bottles_of_beer.to_s + " bottles of beer on the wall. " +
bottles_of_beer.to_s + " bottles of beer. Take one pass it
around, "
+ bottles_of_beer2.to_s + " bottles of beer."
end

puts “Alright! Who drank the last bottle of beer!”

refrain = ’ bottles of beer’
refrain1 = ’ on the wall! ’

bottles = 99
bottles.downto(1) do |i|
puts i.to_s + refrain + refrain1 + i.to_s + refrain
puts ‘you take one down and pass it around’
puts (i-1).to_s + refrain + refrain1
end

Type 1 then hit enter each time you want to remove a beer. I am new at
this but it works.

bottles_of_beer = 99
gets.chomp
while bottles_of_beer > 1 do
gets.chomp
print bottles_of_beer.to_s + ’ Bottles of Beer on the wall, ’ +
bottles_of_beer.to_s + ’ Bottles of Beer’
bottles_of_beer -= 1
puts ’ You take one down, pass it around, ’ + bottles_of_beer.to_s +
’ Bottles of Beer on the wall.’
if
bottles_of_beer == 1

 print 'No more Bottles of Beer on the wall, no more Bottles of

Beer!’ + ’ Go to the store and buy some more, get more Bottles Beer for
the wall!’
end
end

based on book knowledge up to this point…

99 bottles

line1 = ’ bottles of beer on the wall,’
line2 = ’ bottles of beer,’
line3 = ‘take one down, pass it around,’
line4 = ’ bottles of beer on the wall!’
line5 = ‘1 more bottle of beer on the wall!’
line6 = ‘1 more bottle of beer on the wall’
line7 = ‘1 more bottle of beer,’
line8 = ‘take it down, pass it around,’
line9 = ‘no more bottles of beer on the wall!’

bottles = 99

while bottles >= 3
puts bottles.to_s + line1
puts bottles.to_s + line2
puts line3
puts (bottles - 1).to_s + line4
puts ‘’

bottles = (bottles - 1)
end

puts ‘2’ + line1
puts ‘2’ + line2
puts line3
puts line5
puts ‘’
puts line6
puts line7
puts line8
puts line9
puts ‘’
puts ‘The End’
puts ‘’

bottles = 100
while bottles > 1 do
puts “#{bottles} of beer on the wall!”
puts “#{bottles} of beer on the wall!”
puts “You take one down, you pass it around,”
bottles = bottles - 1
puts “And you have #{bottles} bottles of beer on the wall”
end
puts “No more Bottles of Beer on the wall, no more Bottles of
Beer! Go to the store and buy some more, get more Bottles Beer for
the wall!”

I just came up with this one. Works fine unless i missing something why
you have to do to_s or why you make code so complicated.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs