mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Allow calling a private method with self.
This makes it consistent with calling private attribute assignment methods, which currently is allowed (e.g. `self.value =`). Calling a private method in this way can be useful when trying to assign the return value to a local variable with the same name. [Feature #11297] [Feature #16123]
This commit is contained in:
parent
17a1366399
commit
7fbd2f7cc2
Notes:
git
2019-09-20 03:25:07 +09:00
5 changed files with 9 additions and 4 deletions
|
@ -6773,6 +6773,10 @@ compile_call(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, in
|
||||||
iseq_block_param_id_p(iseq, node->nd_recv->nd_vid, &idx, &level)) {
|
iseq_block_param_id_p(iseq, node->nd_recv->nd_vid, &idx, &level)) {
|
||||||
ADD_INSN2(recv, nd_line(node->nd_recv), getblockparamproxy, INT2FIX(idx + VM_ENV_DATA_SIZE - 1), INT2FIX(level));
|
ADD_INSN2(recv, nd_line(node->nd_recv), getblockparamproxy, INT2FIX(idx + VM_ENV_DATA_SIZE - 1), INT2FIX(level));
|
||||||
}
|
}
|
||||||
|
else if (private_recv_p(node)) {
|
||||||
|
ADD_INSN(recv, nd_line(node), putself);
|
||||||
|
flag |= VM_CALL_FCALL;
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
CHECK(COMPILE(recv, "recv", node->nd_recv));
|
CHECK(COMPILE(recv, "recv", node->nd_recv));
|
||||||
}
|
}
|
||||||
|
|
|
@ -260,7 +260,7 @@ end
|
||||||
describe "Invoking a private getter method" do
|
describe "Invoking a private getter method" do
|
||||||
it "does not permit self as a receiver" do
|
it "does not permit self as a receiver" do
|
||||||
receiver = LangSendSpecs::PrivateGetter.new
|
receiver = LangSendSpecs::PrivateGetter.new
|
||||||
-> { receiver.call_self_foo }.should raise_error(NoMethodError)
|
-> { receiver.call_self_foo }.should_not raise_error(NoMethodError)
|
||||||
-> { receiver.call_self_foo_or_equals(6) }.should raise_error(NoMethodError)
|
-> { receiver.call_self_foo_or_equals(6) }.should raise_error(NoMethodError)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -673,7 +673,7 @@ class TestMethod < Test::Unit::TestCase
|
||||||
assert_nothing_raised { mv3 }
|
assert_nothing_raised { mv3 }
|
||||||
|
|
||||||
assert_nothing_raised { self.mv1 }
|
assert_nothing_raised { self.mv1 }
|
||||||
assert_raise(NoMethodError) { self.mv2 }
|
assert_nothing_raised { self.mv2 }
|
||||||
assert_nothing_raised { self.mv3 }
|
assert_nothing_raised { self.mv3 }
|
||||||
|
|
||||||
v = Visibility.new
|
v = Visibility.new
|
||||||
|
|
|
@ -2479,7 +2479,8 @@ class TestModule < Test::Unit::TestCase
|
||||||
assert_include(methods, :#{method}, ":#{method} should be private")
|
assert_include(methods, :#{method}, ":#{method} should be private")
|
||||||
|
|
||||||
assert_raise_with_message(NoMethodError, "private method `#{method}' called for main:Object") {
|
assert_raise_with_message(NoMethodError, "private method `#{method}' called for main:Object") {
|
||||||
self.#{method}
|
recv = self
|
||||||
|
recv.#{method}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
|
@ -538,7 +538,7 @@ class TestRefinement < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_main_using_is_private
|
def test_main_using_is_private
|
||||||
assert_raise(NoMethodError) do
|
assert_raise(NoMethodError) do
|
||||||
eval("self.using Module.new", Sandbox::BINDING)
|
eval("recv = self; recv.using Module.new", Sandbox::BINDING)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue