4 ms·
> Are you using iterators like .each or .map, or are you using a normal, index-based for loop? I doubt you mean anything very judgmental by 'normal', but this
by telemachos 14y ago
> Are you using iterators like .each or .map, or are you using a normal, index-based for loop?
I doubt you mean anything very judgmental by 'normal', but this still jumped out at me. In Ruby an index-based for loop is pretty unusual. The for loop is non-idiomatic, and it also has a scope leak: https://gist.github.com/3232118 https://gist.github.com/3232118.
- jazzychad 14y agohmm, my apologies if that sounded judgmental. I started coding a long time ago, so doing for loops was a very fundamental part of learning how to code. I (incorrectly?) assumed most people learned that form of looping first.
- telemachos 14y agoAgain, I didn't really think you were being judgmental. It's just that in Ruby, the for loop definitely doesn't have pride of place at all. You're may be right that most programmers learn languages where for is more common, but I wonder about people who start with Ruby. That is, do they think for is weirdly primitive when they start doing Javascript? Anyhow, no offense taken at all. But I would be shocked if any Rubyist did use for.
- bunderbunder 14y agoEven in languages like C# the standard for-loop seems to be heading in the direction of being considered harmful. Its status hasn't gone nearly as far along the path as for Ruby, but the tide's turning that way. As far as creating a new array (or several new arrays) goes, there are some new considerations such as composability that are starting to be valued more highly than efficiency. And pure functions are more composable than ones that mutate the existing data. Granted, the n < 0 case could still be handled in a way that's more efficient but still pure by allocating an array and filling it backward rather than using two reverses and an iterator.
- djur 14y agoThere actually _is no_ "normal, index-based" for loop in Ruby, only a for ... in iteration construct. Most C-style for loops can be implemented as #times or #up_to/#down_to/#step iterations.