Skip to content

Commit 6a05406

Browse files
itaratoandrykonchin
authored andcommitted
Add specs for method visibility in some corner cases.
1 parent 66ca27b commit 6a05406

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

spec/ruby/core/module/define_method_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,36 @@ 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 the new method public when the definition frame self is different from the target" do
504+
foo_class = Class.new do
505+
private def bar
506+
"public"
507+
end
508+
end
509+
510+
foo = foo_class.new
511+
foo.singleton_class.define_method(:bar, foo.method(:bar))
512+
513+
foo.bar.should == "public"
514+
end
515+
516+
it "defines the new method according to the scope when the definition context is the same" do
517+
FooWithOneBar = Class.new do
518+
def bar; end
519+
end
520+
521+
foo = FooWithOneBar.new
522+
523+
class << foo
524+
private
525+
define_method(:bar, FooWithOneBar.new.method(:bar))
526+
end
527+
528+
-> { foo.bar }.should raise_error(NoMethodError)
529+
530+
Object.class_eval { remove_const(:FooWithOneBar) }
531+
end
502532
end
503533

504534
describe "Module#define_method" do

0 commit comments

Comments
 (0)