through recent offline discussions a Ruby idiom drew my attention: There are
methods that belong to a pair of resource allocator and deallocator (like
File#open and File#close) where the resource allocator (“open”) can be used
with and without a block. When used with a block, the resource deallocator
(“close”) must not be called, because that is done after the block exits via
"ensure".
Now, to me this seems like mixing different behaviors in a single method.
Having methods that do different things depending on the argument list is
often regarded bad OO. I’m interested to hear why this approach was taken.
Was it to reduce the number of method names one needs to memorize? What do
others think?
with and without a block. When used with a block, the resource deallocator
(“close”) must not be called, because that is done after the block exits via
“ensure”.
Now, to me this seems like mixing different behaviors in a single method.
Having methods that do different things depending on the argument list is
often regarded bad OO. I’m interested to hear why this approach was taken.
Obviously I can’t say *why" it was done. But it feels OK to me
because the block form effectively makes the method a control
structure, like while or if. They just change how your code
behaves, one doesn’t really think of passing in the code to while or
if. Of course if one passed in a Proc.new {|x| … }, that would
feel different.