Skip to content

Commit

Permalink
Make Module#{public,private,protected,module_function} return arguments
Browse files Browse the repository at this point in the history
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]
  • Loading branch information
jeremyevans authored and eregon committed Nov 29, 2021
1 parent d36574a commit 81935b9
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 32 deletions.
12 changes: 10 additions & 2 deletions core/main/private_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@
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
Expand Down
13 changes: 11 additions & 2 deletions core/main/public_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,19 @@
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
Expand Down
40 changes: 28 additions & 12 deletions core/module/module_function_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,23 @@ def test3() end
m.respond_to?(:test3).should == false
end

it "returns the current module" do
x = nil
m = Module.new do
def test() end
x = module_function :test
ruby_version_is ""..."3.1" do
it "returns self" do
Module.new do
def foo; end
module_function(:foo).should equal(self)
end
end
end

x.should == m
ruby_version_is "3.1" do
it "returns argument or arguments if given" do
Module.new do
def foo; end
module_function(:foo).should equal(:foo)
module_function(:foo, :foo).should == [:foo, :foo]
end
end
end

it "creates an independent copy of the method, not a redirect" do
Expand Down Expand Up @@ -160,13 +169,20 @@ def test2() end
m.respond_to?(:test2).should == true
end

it "returns the current module" do
x = nil
m = Module.new {
x = module_function
}
ruby_version_is ""..."3.1" do
it "returns self" do
Module.new do
module_function.should equal(self)
end
end
end

x.should == m
ruby_version_is "3.1" do
it "returns nil" do
Module.new do
module_function.should equal(nil)
end
end
end

it "stops creating module functions if the body encounters another toggle " \
Expand Down
24 changes: 19 additions & 5 deletions core/module/private_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,25 @@ class << obj
:module_specs_public_method_on_object_for_kernel_private)
end

it "returns self" do
(class << Object.new; self; end).class_eval do
def foo; end
private(:foo).should equal(self)
private.should equal(self)
ruby_version_is ""..."3.1" do
it "returns self" do
(class << Object.new; self; end).class_eval do
def foo; end
private(:foo).should equal(self)
private.should equal(self)
end
end
end

ruby_version_is "3.1" do
it "returns argument or arguments if given" do
(class << Object.new; self; end).class_eval do
def foo; end
private(:foo).should equal(:foo)
private([:foo, :foo]).should == [:foo, :foo]
private(:foo, :foo).should == [:foo, :foo]
private.should equal(nil)
end
end
end

Expand Down
24 changes: 19 additions & 5 deletions core/module/protected_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,25 @@ class << ModuleSpecs::Parent
:module_specs_public_method_on_object_for_kernel_protected)
end

it "returns self" do
(class << Object.new; self; end).class_eval do
def foo; end
protected(:foo).should equal(self)
protected.should equal(self)
ruby_version_is ""..."3.1" do
it "returns self" do
(class << Object.new; self; end).class_eval do
def foo; end
protected(:foo).should equal(self)
protected.should equal(self)
end
end
end

ruby_version_is "3.1" do
it "returns argument or arguments if given" do
(class << Object.new; self; end).class_eval do
def foo; end
protected(:foo).should equal(:foo)
protected([:foo, :foo]).should == [:foo, :foo]
protected(:foo, :foo).should == [:foo, :foo]
protected.should equal(nil)
end
end
end

Expand Down
25 changes: 19 additions & 6 deletions core/module/public_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,25 @@
:module_specs_private_method_on_object_for_kernel_public)
end

it "returns self" do
(class << Object.new; self; end).class_eval do
def foo; end
private :foo
public(:foo).should equal(self)
public.should equal(self)
ruby_version_is ""..."3.1" do
it "returns self" do
(class << Object.new; self; end).class_eval do
def foo; end
public(:foo).should equal(self)
public.should equal(self)
end
end
end

ruby_version_is "3.1" do
it "returns argument or arguments if given" do
(class << Object.new; self; end).class_eval do
def foo; end
public(:foo).should equal(:foo)
public([:foo, :foo]).should == [:foo, :foo]
public(:foo, :foo).should == [:foo, :foo]
public.should equal(nil)
end
end
end

Expand Down

0 comments on commit 81935b9

Please sign in to comment.