From: "jeremyevans0 (Jeremy Evans) via ruby-core" Date: 2023-07-13T17:18:32+00:00 Subject: [ruby-core:114169] [Ruby master Bug#19749] Confirm correct behaviour when attaching private method with `#define_method` Issue #19749 has been updated by jeremyevans0 (Jeremy Evans). I don't think my discussion from comment #7 was considered during the dev meeting. Discussion focused on general `define_method` behavior, which I don't think is a bug. I should have been more clear in the dev meeting ticket exactly what I would like to fix. Here's a reduced example showing the problem: ```ruby class A def a; end private def b; end m = instance_method(:b) define_method(:c, m) # defines public method define_method(:b, m) # defines private method, should define public method private m = instance_method(:a) define_method(:d, m) # defines private method define_method(:a, m) # defines public method, should define private method p public_instance_methods(false) # => [:c, :a] # should be [:c, :b] p private_instance_methods(false) # => [:b, :d] # should be [:a, :d] end ``` I think this particular behavior of not changing visibility of a method when calling `define_method` with the same method body is a bug that should be fixed. ---------------------------------------- Bug #19749: Confirm correct behaviour when attaching private method with `#define_method` https://bugs.ruby-lang.org/issues/19749#change-103855 * 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/