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

How do I do that in ruby?

1
  • +1 for asking how to move the turtle in logo! Commented Aug 23, 2010 at 23:29

4 Answers 4

11
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 
Sign up to request clarification or add additional context in comments.

Comments

4
foo = [3, 6, 3] foo.each do |a| puts a end 

Comments

2
foo = [1, 2, 3] foo.each do |x| puts x end 

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.