Feature #12495
closedMake "private" return the arguments again, for chaining
Description
Ruby 2.1 introduced the feature to make def foo return the symbol, so this could be used by things like private (see #3753):
You could use the same method to create your own decorators (name borrowed from Python)
Currently, this would work but cached private def foo() would not. private (and all other modifier functions) return the class on which it was called. It would be nice to exterminate those order-dependencies.
The attached patch fixes this. It includes three modes:
No arguments: return nil:
One argument: return the symbol. The would be the most common use case for this example.
Multiple arguments: return an array of the arguments:
Files
Updated by herwinw (Herwin Quarantainenet) about 10 years ago
The patch is written for Ruby 2.3.1, just because the tests were broken in trunk.
Updated by nobu (Nobuyoshi Nakada) about 10 years ago
- Status changed from Open to Feedback
Only private, not public, protected and module_function?
Herwin Quarantainenet wrote:
The patch is written for Ruby 2.3.1, just because the tests were broken in trunk.
How broken?
Updated by herwinw (Herwin Quarantainenet) about 10 years ago
Only
private, notpublic,protectedandmodule_function?
private, public and protected all use set_visibility, that's where the change is implemented, private was just used as an example. module_function/rb_mod_modfunc has not been updated, but that's an easy fix.
How broken?
Something with the openssl tests. Travis showed them red as well, but thinks it's okay by now. The comment was intended to justify any file offsets, further discussion about it would be rather irrelevant to this case.
Updated by jeremyevans0 (Jeremy Evans) over 4 years ago
- Status changed from Feedback to Open
I think it makes sense to modify private/protected/public/module_function to be like def and return the receiver. I've submitted a pull request for this that builds on @herwinw's patch: https://github.com/ruby/ruby/pull/5037
Updated by larskanis (Lars Kanis) over 4 years ago
I stumbled several times upon this issue. The semantics as described above are OK, IMHO. It would be nice if the patch could be merged.
Updated by Eregon (Benoit Daloze) over 4 years ago
Updated by matz (Yukihiro Matsumoto) over 4 years ago
Sounds OK. Accepted. The side effect should be trivial.
Matz.
Updated by baweaver (Brandon Weaver) over 4 years ago
matz (Yukihiro Matsumoto) wrote in #note-7:
Sounds OK. Accepted. The side effect should be trivial.
Matz.
Oh lovely, very fond of this idea, and had mentioned it as a warning in some of my writings around it. This will definitely be great for consistency. Give me a bit and I can have a piece out on it later, think it'll go for 3.1?
Updated by jeremyevans (Jeremy Evans) over 4 years ago
- Status changed from Open to Closed
Applied in changeset git|75ecbda438670ec12641d1324d0e81a52ee02e0a.
Make Module#{public,private,protected,module_function} return arguments
Previously, each of these methods returned self, but it is
more useful to return arguments, to allow for simpler method
decorators, such as:
Where cached sets up caching for the method.
For each of these methods, the following behavior is used:
- No arguments returns nil
- Single argument is returned
- Multiple arguments are returned as an array
The single argument case is really the case we are trying to
optimize for, for the same reason that def was changed to return
a symbol for the method.
Idea and initial patch from Herwin Quarantainenet.
Implements [Feature #12495]