Frage

for current_iteration_number in 99..1 do      
   puts "#{current_iteration_number} of beer on the wall. 
   #{current_iteration_number} of beer. Take one down, 
   pass it around #{current_iteration_number} of beer."    
end
War es hilfreich?

Lösung

Instead of:

for current_iteration_number in 99..1 do

you can do:

for current_iteration_number in 99.downto(1) do

Andere Tipps

Ruby doesn't work like that for reverse ranges.

You should use some other way of doing it.

For instance:

99.downto(1).each do |current_iteration_number|
    puts "#{current_iteration_number} of beer on the wall. #{current_iteration_number} of beer. Take one down, pass it around #{current_iteration_number} of beer."
end

You can do it like this too

a= 99..1

(a.first).downto(a.last).each do |current_iteration_number|
puts "#{current_iteration_number} of beer on the wall. 
   #{current_iteration_number} of beer. Take one down, 
   pass it around #{current_iteration_number} of beer.
end
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top