by j_baker · 16y ago · 0 comments
However, I get the feeling that they're not Ruby's strongest feature. Python's coolest features (metaclasses, descriptors, and other things) wouldn't make sense if you'd just read a blog post on them. I suspect Ruby is the same way.
But once you have closures with a light-weight syntax, as Ruby has, the APIs start to look a lot more different.
For example, take this example ...
v = [ x for x in collection if x % 2 == 0 ]
for item in v:
print item
In Ruby the equivalent would be ... collection.find_all{|x| x % 2 == 0}.each do |item|
puts item
end
Yes, the Python example is elegant, but Ruby doesn't need extra baked-in features like list-comprehensions. It doesn't need a bunch of other features as well, like generators, or generator expressions, or with statements.There are a lot of PEPs in Python that cover use-cases for Ruby's blocks, trouble is there are still use-cases that aren't covered.
Guido is partially right though ... adding blocks in Python wouldn't be pythonic because blocks wouldn't be orthogonal with lots of other Python features. They should've been added from the start, and now it's kind of late.
Perhaps the strongest feature is the underlying architecture that allows for blocks, the general idea that behavior is a data type you can pass around.
And since you can alter the properties of objects at different levels (instances, classes, meta-classes), you can alter not just the more conventional values (int, strings) but an object's (and system of objects) very nature.