summaryrefslogtreecommitdiff
path: root/spec/ruby/core/main
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2021-10-27 10:35:54 -0700
committerJeremy Evans <code@jeremyevans.net>2021-11-18 09:47:40 -0800
commit75ecbda438670ec12641d1324d0e81a52ee02e0a (patch)
tree6ca80769c4b2ebdcababc30759ae7872275c19b8 /spec/ruby/core/main
parentec574ab3453709490b53b5cc761ec158103fe42a (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 'spec/ruby/core/main')
-rw-r--r--spec/ruby/core/main/private_spec.rb12
-rw-r--r--spec/ruby/core/main/public_spec.rb13
2 files changed, 21 insertions, 4 deletions
diff --git a/spec/ruby/core/main/private_spec.rb b/spec/ruby/core/main/private_spec.rb
index 78c5d287d4..cac0645b40 100644
--- a/spec/ruby/core/main/private_spec.rb
+++ b/spec/ruby/core/main/private_spec.rb
@@ -32,8 +32,16 @@ describe "main#private" do
end
end
- it "returns Object" do
- eval("private :main_public_method", TOPLEVEL_BINDING).should equal(Object)
+ ruby_version_is ''...'3.1' do
+ it "returns Object" do
+ eval("private :main_public_method", TOPLEVEL_BINDING).should equal(Object)
+ end
+ end
+
+ ruby_version_is '3.1' do
+ it "returns argument" do
+ eval("private :main_public_method", TOPLEVEL_BINDING).should equal(:main_public_method)
+ end
end
it "raises a NameError when at least one of given method names is undefined" do
diff --git a/spec/ruby/core/main/public_spec.rb b/spec/ruby/core/main/public_spec.rb
index bfc27a9e80..91f045dbab 100644
--- a/spec/ruby/core/main/public_spec.rb
+++ b/spec/ruby/core/main/public_spec.rb
@@ -32,10 +32,19 @@ describe "main#public" do
end
end
- it "returns Object" do
- eval("public :main_private_method", TOPLEVEL_BINDING).should equal(Object)
+ ruby_version_is ''...'3.1' do
+ it "returns Object" do
+ eval("public :main_private_method", TOPLEVEL_BINDING).should equal(Object)
+ end
end
+ ruby_version_is '3.1' do
+ it "returns argument" do
+ eval("public :main_private_method", TOPLEVEL_BINDING).should equal(:main_private_method)
+ end
+ end
+
+
it "raises a NameError when given an undefined name" do
-> do
eval "public :main_undefined_method", TOPLEVEL_BINDING