DEV Community

Renan Lourençoni Nobile
Renan Lourençoni Nobile

Posted on

Why avoid Ruby's for .. in?

So I'm starting to learn Ruby and found a great resource, Learn Ruby in Y minutes. It's great, very fast paced as I expected, since I'm an experienced JS developer.

But I got to the Loops part and something really caught my attention. It says the following syntax is preferable:

(1..5).each do |counter| to this one for counter in 1..5. Now, it may be my javascript developer side speaking, but the second form looks better/cleaner to me. Am I insane or is there a reason for the first form to be more common than the second one? Are there any performance differences?

Thank you all.

Top comments (13)

Collapse
 
dfockler profile image
Dan Fockler

One thing to think about is that the for counter in 1..5 is really only used for looping syntax, whereas most other ruby Enumerable methods like #map and #reduce have similar syntax as #each, so consistency is likely a factor in why people prefer one over the other.

Collapse
 
renannobile profile image
Renan Lourençoni Nobile

Looking from that perspective it's a good option to use each.

Thank you

Collapse
 
andy profile image
Andy Zhao (he/him)

Given this:

banana = { name: "banana", flavor: "sweet", texture: "soft" }
apple = { flavor: "sweet", texture: "crispy" }
grapefruit = { name: "grapefruit", flavor: "sour", texture: "juicy" }

fruit_basket = [banana, apple, grapefruit]

for fruit in fruit_basket
  puts "#{fruit[:name]} is #{fruit[:flavor]}"
end

fruit_basket.each do |fruit|
  puts "#{fruit[:name]} is #{fruit[:flavor]}"
end

I would say, yeah there's not much of a difference in terms of readability.

BUT there is a good reason. Try running this code example:

Basically, for..in loop can redefine things that you didn't intend to, and that can cause problems.

Reference: Tutorials Point - Ruby Loops

Collapse
 
renannobile profile image
Renan Lourençoni Nobile

Hey Andy, I just ran the code and it really makes much more sense to use each method. I believe there are use cases for the for .. in syntax right?

Anyway, thank you for the information.

Collapse
 
renannobile profile image
Renan Lourençoni Nobile

I'm on mobile right now, when I get a chance I'll run it on my laptop, but knowing that both loops act differently and for in might cause inconstancy is a great point towards each.

Thank you

Collapse
 
johncarroll profile image
John Carroll • Edited

I believe there are use cases for the for .. in syntax right?

So I don't have anything against the for .. in syntax personally, but no, there are no special use cases for it. I mean, I'm sure if you tried you could construct a situation where for might be preferable to .each, but really, it's presence in the language at this point is just vestigial.

I'm not sure I've ever seen it used outside of a tutorial.

I was amused to see that Crystal-lang went so far as to remove it altogether.

Collapse
 
ben profile image
Ben Halpern

Performance differences seem negligible gist.github.com/jodosha/229951

As for better/cleaner, Rubyists definitely love .each, and for practical purposes, it's probably worth just drinking the kook-aid rather than stirring things up with Ruby dogma.

But I'm happy to hear the bikeshedding on this. 😄

Collapse
 
phallstrom profile image
Philip Hallstrom

I've been writing Ruby for 12-13 years and have never once used or even come across for in real life. Definitely bikeshedding :)

More seriously though, you can't pass a lambda to a for loop like you can each that I'm aware of. Which is a huge part of Ruby's Enumerable ecosystem.

Collapse
 
renannobile profile image
Renan Lourençoni Nobile

Firstly, thank you for the explanation.

Secondly, I really like the for .. in syntax, although in Ruby (don't know if it's the same for JavaScript), counter cleanup is non existent.

Thirdly (is that a word?): What is rubocop?

Collapse
 
briankephart profile image
Brian Kephart

Rubocop link. Rubocop can analyze your code, alert you to syntax errors, and suggest style improvements.

Thread Thread
 
renannobile profile image
Renan Lourençoni Nobile

Awesome! Is there a plugin version for VS Code?

Thread Thread
 
briankephart profile image
Brian Kephart

Almost definitely. I use Atom, and there's a package for it in that editor. I'd be shocked if VSC didn't have one.

Thread Thread
 
renannobile profile image
Renan Lourençoni Nobile

Thank you very much for the tip, I'll make sure to look for that