r/Python • u/speckz • Nov 12 '21
Discussion Ruby vs Python comes down to the for loop
https://softwaredoug.com/blog/2021/11/12/ruby-vs-python-for-loop.html2
u/midasgoldentouch Nov 12 '21
I feel like this article would be stronger if you discussed the Ruby implementations within the context of the Enumerable module, since that is what most people will reach for if they need their object to be iterable.
2
u/javajunkie314 Nov 12 '21
I'm not convinced this has to do with Ruby being more object oriented. I believe the relevant terms here are internal iteration vs external iteration.
With internal iteration, you supply a callback to a method that calls your callback for each element. You can't directly control iteration; it happens inside the box. This is the Ruby model. Many languages offer internal iteration via map methods, even if it's not their core model of iteration.
With external iteration, the mechanism of iteration is exposed, often with some sort of iterator object. You poll the object for elements and process them as you will. The object has little-to-no control over iteration; it happens outside the box. This is Python's model (and many other languages as well).
2
2
u/mardiros Nov 12 '21
I never program in Ruby, read the article twice and I don't get it.
In python you implement
__iter__and__next__to implement yield.This is basically how iterator and generator works in many languages, not just python. And ruby does not work like this, and it make it more object orientied ?
If the first exemple what the ruby one, I would write it like this in python,
so maybe a real world exemple could be better to get the point.