3 ms·
While people have pointed out that this is not idiomatic: class Stuff def initialize @a_list = [1, 2, 3, 4] end def each for item in
by dragonwriter 5y ago
While people have pointed out that this is not idiomatic:
class Stuff
def initialize
@a_list = [1, 2, 3, 4]
end
def each
for item in @a_list
yield item
end
end
end
I haven't seen anyone point out the deepest reason why: unlike in many languages, “for” is not a basic, low-level construct in Ruby, it is syntax sugar. That is:
for x in y
x.stuff
more_stuff
end
is sugar for:
y.each do |x|
x.stuff
more_stuff
end
(Also, for quite a long time, for...in also had runtime overhead , so it was slower syntactic sugar.)
for...in is rarely used (it is syntactic sugar that is neither more concise bor more specific in intent, but more familiar to people with experience in non-Ruby languages), and mostly on scripts that consume but don’t implement collections. If you are getting deep enough to be implementing an #each method for a custom collection, that's an odd place to use for.
- saghm 5y agoI'm not sure if this was ever changed, but I remember running into something surprising regarding variable scoping with for loops (I think it was during an internship in 2014, so I think Ruby 2.x with some relatively low value for "x"). Unfortunately I can't remember exactly what it was; I think it was something about variables defined in the for loop still being in scope afterwards? That doesn't seem like it should be the case if it's just syntactic sugar for `each` though, so I might be remembering wrong
- dragonwriter 5y agoYes, for loops (and similar statements) don't introduce scope, so its not quite simple sugar for .each, and there are a few cases where you might use them over each for that property (if you are mutating values that you want to access afterward, it can be more concise than each) but they are mostly anti-idiomatic.
- necovek 5y agoBut that actually makes it more similar to Python. In Python, `for value in iterator: x(value)` is syntactic sugar for (roughly) while True: try: value = next(iterator) except IterationError: break x(value) Basically, it seems that `while conditional` are both low-level control flow constructs in both: how do they differ between languages?
- alexfrydl 5y agoRuby's “for … in” is syntax sugar in the sense that is just another way to spell an identical piece of code. This would be like if Python let you write “foreach” instead of “for” if you wanted to. Another example in Ruby is how you can say “unless” instead of “if not”. Python's “for … in” is syntax sugar in the sense that is an abstraction over constantly writing out the iterator protocol.
- presentation 5y agoIf anything I found Ruby's breadth of syntax sugar a defining characteristic of it - you can express the same program in a million different ways based on the programming paradigm you wish to emulate, or even make a DSL of your own to express the feelings in your heart. Not that I think that's necessarily a good thing though...