Pratik said: "This looks interesting. But I'm not sure if this pattern
belongs to the core or not. You should probably spark a discussion in
the core ML - http://groups.google.com/group/rubyonrails-core"
The Subject can be found here / with a few Pros and Cons...
What about using Object#try and avoiding the explicit &block argument?
This avoid to instantiate a Proc object for each method execution, if any block isn’t passed and the enumerable is empty the VM will raise a LocalJumpError.
Yeah creating a proc every time isn’t needed and much cleaner this way. I do think that it’s better to use #blank? as was used in Ryan’s patch. Still waiting for that ticket
Wouldn't this method be better named "ifempty"? If you call it "else" it makes me wonder where the other part of the clause is? Certainly it reads nicely in expressions like this:
results.each do |x|
puts x
end.else do
puts "none found"
end
But each isn't exactly an if statement. I can guess what it means in the context above, but it doesn't read right.
And what if you wanted to use it on its own?
results.else do
puts "none found"
end
In the case above it makes little sense. "ifempty" seems to read a little better.