Skip to content

method_added hook incompatible with delegate private #47999

@crevete

Description

@crevete

Steps to reproduce

delegate with option private: true seems not compatible with the method_added hook.

In the following snippet, delegate creates a private method #foo. However the method_added hook is called BEFORE the delegate code changes its visibility to false.

class MyTestClass
  def foo = "foo"
end

class MyClass
  attr_reader :my_test_class

  def initialize(my_test_class)
    @my_test_class = my_test_class
  end

  def self.method_added(method_name)
    puts "#{method_name} is public: #{public_method_defined?(method_name, true)}"
    # super(method_name)
  end

  delegate :foo, to: :my_test_class, private: true
end

The source code of delegate shows that: we always define a method as public first, then change its visibility. But the method_added hook is executed between these 2 steps.

Is it possible to take into account the method visibility when defining a method? Thanks

Expected behavior

When loading MyClass, the output is foo is public: false

Actual behavior

When loading MyClass, the output is foo is public: true

System configuration

Rails version: 7.0.4.3

Ruby version: 3.2.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions