[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: 99 bottles of beer...pg57

Shiloh Madsen

11/7/2006 5:51:00 PM

Hi Jamison, I came up with a shorter version. I think it is more
efficient, but I am about as new as you are. Here was my code:

bottles = 99
while bottles > 0
puts bottles.to_s + " bottles of beer on the wall, " + bottles.to_s +
" bottles of beer. Take one down and pass it around, " + (bottles
= bottles - 1).to_s + " bottles of beer."
end

On 11/7/06, jamison edmonds <jamison2000e@yahoo.com> wrote:
> Hi
>
>
>
> I'm reading the book Learn to Program, by Chris Pine. I
> wanted to see if my program 99 bottles of beer on the wall, from (pg.57 or http://pine.fm/LearnToProgram/?... ) is
> o.k.?
>
>
> An online search thru Google found Ruby-Talk, and I
> instantly bookmarked it.
>
>
> Now I'm on the list.
>
>
> Any thoughts would be appreciated ;)
>
>
> Hears my try at it
>
>
> Thanks
>
>
> puts
> puts '99, Bottles of beer on the wall, 99 bottles of beer,'
> puts 'take one down, pass it around,'
> first = 99
> while first != 1
> if first != 1
> first = first -1
> end
> puts first.to_s + ' Bottles of beer on the wall, ' + first.to_s + ' bottles of beer,'
> puts 'take one down pass it around,'
> end
> puts '0,Bottles of beer on the wall ;)'
>
>
>
>

3 Answers

Gavin Kistner

11/7/2006 6:10:00 PM

0

On 11/7/06, jamison edmonds <jamison2000e@yahoo.com> wrote:
> Any thoughts would be appreciated ;)

Instead of using a 'while' loop (unless that's what the book is
teaching you) I think the following is more Ruby-esque:

99.downto( 1 ) do |num|
puts "#{num} bottles of beer on the wall, #{num} bottles of beer,"
puts "take one down, pass it around,"
end
puts '0,Bottles of beer on the wall ;)'

Note the use of string interpolation also instead of .to_s and +

Gavin Kistner

11/7/2006 6:13:00 PM

0

Phrogz wrote:
> 99.downto( 1 ) do |num|
> puts "#{num} bottles of beer on the wall, #{num} bottles of beer,"
> puts "take one down, pass it around,"
> end
> puts '0,Bottles of beer on the wall ;)'

Oops, I am, of course, missing one line of the song, and a nice newline
for breaks. Should be more like:

99.downto( 1 ) do |num|
puts
puts "#{num} bottles of beer on the wall, #{num} bottles of beer,"
puts "take one down, pass it around,"
puts "#{num-1} bottles of beer on the wall" unless num == 1
end
puts '0 bottles of beer on the wall ;)'

Reprisal

11/7/2006 6:42:00 PM

0

you could drop the unless and the final line. ( num = 1, num-1 = 0 ;)

99.downto( 1 ) do |num|
puts
puts "#{num} bottles of beer on the wall, #{num} bottles of beer,"
puts "take one down, pass it around,"
puts "#{num-1} bottles of beer on the wall"
end

Altho I think the book hasn't covered the enumeration type stuff yet
like downto.


On Nov 7, 2006, at 1:15 PM, Phrogz wrote:

> Phrogz wrote:
>> 99.downto( 1 ) do |num|
>> puts "#{num} bottles of beer on the wall, #{num} bottles of beer,"
>> puts "take one down, pass it around,"
>> end
>> puts '0,Bottles of beer on the wall ;)'
>
> Oops, I am, of course, missing one line of the song, and a nice
> newline
> for breaks. Should be more like:
>
> 99.downto( 1 ) do |num|
> puts
> puts "#{num} bottles of beer on the wall, #{num} bottles of beer,"
> puts "take one down, pass it around,"
> puts "#{num-1} bottles of beer on the wall" unless num == 1
> end
> puts '0 bottles of beer on the wall ;)'
>
>