From: "mame (Yusuke Endoh) via ruby-core" Date: 2023-07-13T15:06:11+00:00 Subject: [ruby-core:114166] [Ruby master Bug#19749] Confirm correct behaviour when attaching private method with `#define_method` Issue #19749 has been updated by mame (Yusuke Endoh). Discussed at the dev meeting. In conclusion, @matz said he would like to keep the status quo. Method visibility is not an attribute of a method itself, but an attribute of the class to which the method belongs. Therefore, `X.define_method(:bar, method(:foo))` should not inherit the visibility of foo. Currently, `Module#define_method` defines a private/protected method if the context is private/protected *and* if the class/module of the context is equal to the receiver of `define_method`. Otherwise, it defines a public method. This behavior is hard to document, though. @matz discussed the behavior of `define_method` as follows: 1. Ideally, I want `define_method` to always define a public method. However, it is too late to change. 2. The next desirable behavior is to define a public method if the receiver of define_method is explicit (i.e., NODE_CALL), and to follow context visibility if it is not explicit (i.e., NODE_FCALL). However, Matz gave up this idea because @ko1 stated that it was difficult to implement efficiently. 3. The next next desirable is the current behavior. ---------------------------------------- Bug #19749: Confirm correct behaviour when attaching private method with `#define_method` https://bugs.ruby-lang.org/issues/19749#change-103850 * Author: itarato (Peter Arato) * Status: Open * Priority: Normal * ruby -v: 3.3.0 * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- This issue is a special case of https://bugs.ruby-lang.org/issues/19745: Should dynamically added private methods via `.singleton_class.send(:define_method,...` at the top-level be accessible publicly? See the following example: ```ruby def bar; end foo = Object.new foo.singleton_class.define_method(:bar, method(:bar)) foo.bar # No error. ``` The script above runs fine on latest Ruby 3.3. Is this correct to ignore the fact that the visibility in the caller context is the default top-level private visibility? 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 # (NoMethodError)` -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/