diff options
author | Jeremy Evans <[email protected]> | 2021-10-27 10:35:54 -0700 |
---|---|---|
committer | Jeremy Evans <[email protected]> | 2021-11-18 09:47:40 -0800 |
commit | 75ecbda438670ec12641d1324d0e81a52ee02e0a (patch) | |
tree | 6ca80769c4b2ebdcababc30759ae7872275c19b8 /test/ruby/test_module.rb | |
parent | ec574ab3453709490b53b5cc761ec158103fe42a (diff) |
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:
```ruby
cached private def foo; some_long_calculation; end
```
Where cached sets up caching for the method.
For each of these methods, the following behavior is used:
1) No arguments returns nil
2) Single argument is returned
3) 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]
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/5037
Diffstat (limited to 'test/ruby/test_module.rb')
-rw-r--r-- | test/ruby/test_module.rb | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb index be23b84c46..e1524a5d81 100644 --- a/test/ruby/test_module.rb +++ b/test/ruby/test_module.rb @@ -1014,6 +1014,28 @@ class TestModule < Test::Unit::TestCase assert_raise(NoMethodError, /protected method/) {o.aClass2} end + def test_visibility_method_return_value + no_arg_results = nil + c = Module.new do + singleton_class.send(:public, :public, :private, :protected, :module_function) + def foo; end + def bar; end + no_arg_results = [public, private, protected, module_function] + end + + assert_equal([nil]*4, no_arg_results) + + assert_equal(:foo, c.private(:foo)) + assert_equal(:foo, c.public(:foo)) + assert_equal(:foo, c.protected(:foo)) + assert_equal(:foo, c.module_function(:foo)) + + assert_equal([:foo, :bar], c.private(:foo, :bar)) + assert_equal([:foo, :bar], c.public(:foo, :bar)) + assert_equal([:foo, :bar], c.protected(:foo, :bar)) + assert_equal([:foo, :bar], c.module_function(:foo, :bar)) + end + def test_s_constants c1 = Module.constants Object.module_eval "WALTER = 99" |