Feature #10499
closed
Eliminate implicit magic in Proc.new and Kernel#proc
Added by headius (Charles Nutter) about 10 years ago.
Updated almost 4 years ago.
Description
Proc.new and Kernel#proc have a little known feature: if called without a block, they capture whatever block was passed to the current method.
I propose that this feature should be removed, finally, since it:
- Doesn't enhance readability (where is this block coming from?)
- Doesn't reflect any other behavior in Ruby
- Can lead to bugs (call either without a block accidentally and you aren't sure what you'll get)
I believe this was an implementation artifact in MRI, since the most recently-pushed block would still be on global stacks, which is where the logic for proc and Proc.new looked for it.
All argument syntaxes now support &block, which I believe is the correct way to clearly, explicitly capture the incoming block into an object.
Thoughts?
- Category set to core
- Assignee set to matz (Yukihiro Matsumoto)
I agree.
Deprecate first (2.2?), remove afterwards.
This would also simplify things if and when we want to warn/raise on unused blocks when calling user methods.
Adding a deprecation warning would be easy if we can get buy-in from matz.
matz: ball's in your court, I think!
- Copied to Feature #15554: warn/error passing a block to a method which never use a block added
- Target version deleted (
3.0)
Was this change implemented in Ruby 3.0.0?
It looks to me like it was. (And this is causing test failures on libraries that depend on this behavior.)
Ruby 2.7.2:
def foo
Proc.new
end
foo # => ArgumentError
foo { :hi } # => Proc
Ruby 3.0.0:
def foo
Proc.new
end
foo # => ArgumentError
foo { :hi } # => ArgumentError
- Status changed from Open to Closed
- Copied to deleted (Feature #15554: warn/error passing a block to a method which never use a block)
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0