DEV Community

loading...

for/in vs. each in Ruby: More than just syntax

drbragg profile image Drew Bragg ・1 min read

During a recent PR review I found myself having to explain why we would want to use each over for/in in Ruby. Turns out most people think it's just more idiomatic to use each over for but it's much more than that.

With for, the variable you use will be available outside of the loop, while the variable you use in each will only be available in that block.

x = [1, 2, 3]

x.each do |i|
  puts i
end
#...
puts i #=> NameError: undefined local variable or method `i' for main:Object

for i in x
  puts i
end
#...
puts i #=> 3
Enter fullscreen mode Exit fullscreen mode

This is because for does not introduce a new scope but each does via the block you pass it. It’s important to understand scope here because you can really make a mess with for if you’re not careful. ie:

def num
  puts 4
end

num #=> 4

x = [1, 2, 3]

x.each do |num|
  puts num
end
#...
num #=> 4

for num in x
  puts num
end
#...
num #=> 3
Enter fullscreen mode Exit fullscreen mode

I hope this clears up why you should use each over for while working with Ruby. It's a simple thing that can have a rather large impact.

Discussion (0)

pic
Editor guide