Bug #19745
closedConfirm correct behaviour when attaching private method with `#define_singleton_method`
Description
Should dynamically added private methods be accessible publicly?
See the following example?
private def bar; end
foo = Object.new
foo.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)
Updated by jeremyevans0 (Jeremy Evans) over 1 year ago
- Status changed from Open to Closed
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.
Updated by Eregon (Benoit Daloze) over 1 year ago
@itarato (Peter Arato) 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
)
Updated by itarato (Peter Arato) over 1 year ago
- 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`