Skip to content

Commit f5c5c19

Browse files
andrykonchineregon
authored andcommitted
[GR-19220] Fix method visibility in some corner cases.
PullRequest: truffleruby/3947
2 parents f9c1679 + 9af32c6 commit f5c5c19

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

spec/ruby/core/module/define_method_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,33 @@ def foo
499499
Class.new { define_method :bar, m }
500500
}.should raise_error(TypeError, /can't bind singleton method to a different class/)
501501
end
502+
503+
it "defines a new method with public visibility when a Method passed and the class/module of the context isn't equal to the receiver of #define_method" do
504+
c = Class.new do
505+
private def foo
506+
"public"
507+
end
508+
end
509+
510+
object = c.new
511+
object.singleton_class.define_method(:bar, object.method(:foo))
512+
513+
object.bar.should == "public"
514+
end
515+
516+
it "defines the new method according to the scope visibility when a Method passed and the class/module of the context is equal to the receiver of #define_method" do
517+
c = Class.new do
518+
def foo; end
519+
end
520+
521+
object = c.new
522+
object.singleton_class.class_eval do
523+
private
524+
define_method(:bar, c.new.method(:foo))
525+
end
526+
527+
-> { object.bar }.should raise_error(NoMethodError)
528+
end
502529
end
503530

504531
describe "Module#define_method" do

0 commit comments

Comments
 (0)