From 7d630b9c4ed2bf490ac695d93fa91ea1ccaae0c5 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Mon, 31 Jan 2022 11:20:44 -0600 Subject: [PATCH] Test that define_singleton_method uses public In CRuby this logic happens to always be public because the target class (the target object's singleton class) will almost never match the caller's scope. Pending CRuby confirming that this is the intended behavior and making it explicit so the caller's frame is never used for define_singleton_method visibility. --- core/kernel/define_singleton_method_spec.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/core/kernel/define_singleton_method_spec.rb b/core/kernel/define_singleton_method_spec.rb index dc77c3e6f8..f96b0d12ab 100644 --- a/core/kernel/define_singleton_method_spec.rb +++ b/core/kernel/define_singleton_method_spec.rb @@ -96,4 +96,17 @@ def o.define(name) o.define(:foo) { raise "not used" } }.should raise_error(ArgumentError) end + + it "always defines the method with public visibility" do + cls = Class.new + def cls.define(name, &block) + private + define_singleton_method(name, &block) + end + + -> { + cls.define(:foo) { :ok } + cls.foo.should == :ok + }.should_not raise_error(NoMethodError) + end end