foo = [3, 6, 3]
for a in foo:
print a
How do I do that in ruby?
foo = [3, 6, 3]
for a in foo:
print a
How do I do that in ruby?
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
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.