Ruby/Statement/downto
Материал из Wiki.crossplatform.ru
(Различия между версиями)
ViGOur (Обсуждение | вклад) м (1 версия: Импорт выборки материалов по Ruby) |
Текущая версия на 18:00, 13 сентября 2010
Содержание[убрать] |
A downto iterator, which starts at a higher number and loops to a lower number.
grades = [88, 99, 73, 56, 87, 64] sum = 0 0.downto(0) do |loop_index| sum += grades[loop_index] end average = sum / grades.length puts average
Both the Integer and Date classes have downto methods
# String does not have a downto method. # Like upto, it uses a block. 5.downto(1) { |i| print i, " " } # => 5 4 3 2 1
downto method with integer
100.downto(1) { |c| print c, " "; sleep 1 }
downto with block
100.downto(1) { |c| print c, " "; sleep 1 }
number of minutes you want to count down.
def timer( start ) puts "Minutes: " + start.to_s start_time = Time.now puts start_time.strftime("Start to_time: %I:%M:%S %p") start.downto(1) { |i| sleep 60 } end_time = Time.now print end_time.strftime("Elapsed time: %I:%M:%S %p") end timer 10