Should dynamically added private methods be accessible publicly?
See the following example?
privatedefbar;endfoo=Object.newfoo.define_singleton_method(:bar,method(:bar))foo.bar# No error.
The script above runs fine on latest Ruby HEAD. Is this correct to ignore the fact that the added method (method(:bar)) is private?
This came up during a TruffleRuby investigation (https://github.com/oracle/truffleruby/issues/3134) where the result for the same script is: private method 'bar' called for #<Object:0xc8> (NoMethodError)
It is expected that define_singleton_method defines a public singleton method (see #18561). For define_method, visibility can depend on the current default visibility for the class/module scope. In any case, the visibility of the defined method does not depend on the method body (second argument/block). So this would be a bug in TruffleRuby.
@itarato This example is not the same as the linked issue.
I would suggest to open a separate issue to dicuss the original example, which uses define_method.
(although this define_singleton_method example does fail on TruffleRuby, we should fix define_singleton_method)
Subject changed from Confirm correct behaviour when attaching private method with `#define_method`/`#define_singleton_method` to Confirm correct behaviour when attaching private method with `#define_singleton_method`