tags:

views:

107

answers:

4
foo = [3, 6, 3]
for a in foo:
    print a

How do I do that in ruby?

+4  A: 
foo = [3, 6, 3]
foo.each do |a|
  puts a
end
calmh
+2  A: 
foo = [1, 2, 3]
foo.each do |x|
    puts x
end
You
+10  A: 
list = %w( a b c )

# there's a for statement but nobody likes it :P
for item in list
  puts item
end

# so you use the each method with a block instead

# one-liner block
list.each { |item| puts item }

# multi-line block
list.each do |item|
  puts item
end
kch
+1  A: 

Your already have both correct answers about "for"-loop. But in Exactly your example, i'll use:

puts foo

Also you can use this puts' feature in such case:

puts array.map { |i| ...some code...; x }

instead of

array.each { |i| ...some code...; puts x }

for example, if you want to call puts only once.

Nakilon