茴香豆有幾種寫法之 Loop
2015-02-13 本文已影响76人
jProvim
Loop
-
loop
number = 0 loop do break if number > 15 puts "The number is #{number}" number += 1 end
-
until
until number > 30 puts "The number is #{number}" number += 1 end
-
while
while number < 40 do puts "The number is #{number}" number += 1 end
-
for
for i in 1..8 do puts i end
-
times
16.times do |number| puts "The number is #{number}" end
-
times
oneliner5.times { |i| puts i }
-
upto
5.upto 10 do puts 'Hi' end
-
downto
15.downto(10) {|i| puts i }
-
each
list = [0, 1, 2, 3, 4] list.each do |number| puts "The number inside the loop is #{number}" end
-
each
without initializing list(0..15).each do |number| puts "The number inside the loop is #{number}" end
-
for
another formfor number in 0..15 puts "The number inside the loop is #{number}" end
請 牛人 現身, 繼續增加方法!