Project

General

Profile

Actions

Bug #19745

closed

Confirm correct behaviour when attaching private method with `#define_singleton_method`

Added by itarato (Peter Arato) 11 months ago. Updated 10 months ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:114029]

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) 11 months 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) 10 months 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)

Actions #3

Updated by itarato (Peter Arato) 10 months 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`
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0