On Sunday, April 4, 2004, 11:14:19 PM, Ville wrote:

>>>>>> "Dan" == Dan Doel <djd15 / po.cwru.edu> writes:

>     Dan> I'm not the poster of that opinion, but I imagine he was
>     Dan> referring to things like #each and #inject and the Enumerable
>     Dan> module. I don't know Python, so I don't know what the
>     Dan> analogues to these are, or if it has them at all.  Perhaps
>     Dan> whenever he looked at Python, it didn't.

> #each is just a for loop (at least the #each usages I've seen
> here). Python has always had it, but but it didn't probably have
> generators, which provide 'yield' functionality. Well, it has now.

#each is not just a for loop.  #each is the embodiment of Ruby's
ability to provide arbitrary abstraction layers using blocks.
Functionally, it may be a for loop, but suggesting they're equivalent
is missing an important point.

Python has a for loop, as you say.  Ruby doesn't.  Its 'for' keyword
is sugar for #each, and you'll find it's not used often.  Where is
Python's #each_with_index, and #select?  Where is Python's equivalent
of

  File.open('/tmp/xyz', 'w') do |f|
    f.write('blah')
  end                  # file is closed automatically

Where is Python's

  "Hello world".gsub(/(w\w+)/) { |match|
    match.upcase
  }

  # produces "Hello WORLD"
  
Generators do not provide Ruby's 'yield' functionality.  They provide
"expanded for loops", which require explicit invocation, which
(despite the zen) is not in this case a good thing.

Possibly Python does provide all this, or a way to get it.  Just as
you are guilty of approaching Ruby from a Python frame of reference, I
am guilty in the opposite direction.

But Ruby's blocks are far superior to Python's generators, IMO.  They
are not a superficial feature that could just be tacked on to the
langauge to make everyone happy.  Ruby would suck without them :)

Blocks, and their pervasiveness in the Ruby language, provide an
incredibly elegant way of writing high-level code while providing
low-level directives.  It's the best "separation of concerns" I've
seen in any language.

Cheers,
Gavin

PS. Ruby has generators as well, in the standard library (implemented
in Ruby, not in the interpreter).