Ruby/Statement/for

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

For each item in

print "Your pizza comes with "
for item in ["pepperoni", "sausage", "olives"]
  print item + " "
end



general form of the for loop

for variable [, variable...] in collection [do | :]
  code
end



Nested for loop

#!/usr/bin/env ruby
for i in 1..12
  for j in 1..12
    print i.to_s + " x " + j.to_s + " = ", j * i, "\n"
  end
end



Using the for Loop

for value in 1..10
  puts "This is iteration " + value.to_s
end