"Dat Nguyen" <thucdat / hotmail.com> writes:

> #ow to shortenhe following experimental codes?
> 
> bottle = ' bottles'
> 99.downto(1) { |i|
> print "\n", i, bottle, ' of beer on the wall, ', i, bottle, ' of beer', ".\n"
> print 'Take one down, pass it around,', "\n"
> bottle = ' bottle' if i == 2
> print i-1, bottle, ' of beer on the wall.', "\n" if i > 1
> }
> print 'No bottles of beer on the wall.', "\n\n"

The use of NIL to save a variable here is a bit tacky...

     def NIL.n=(n)@n=n;end;def NIL.to_s() %[#@n bottle#{@n==1?"":"s"} of beer];end
     99.downto(1){|NIL.n|print "#{NIL} on the wall, #{NIL}.\n";
     puts "Take one down, pass it around,"};puts "No more bottles of beer on the wall."

Or, slightly tider...


     def NIL.n=(n) @n = n; end
     def NIL.to_s() %[#@n bottle#{@n == 1 ? "" : "s"} of beer]; end

     99.downto(1) { |NIL.n|
       print "#{NIL} on the wall, #{NIL}.\n"
       puts  "Take one down, pass it around,"
     }
     puts "No more bottles of beer on the wall."

Dave