質問

Is it possible to make the Forwardable#def_delegator method respect that the target method is private?

In the following code, when def_delegator is used, Foo#hello is called even though it's private, because the implementation of Forwardable#def_delegator (actually, def_instance_delegator) uses __send__, which doesn't respect a method being private.

require "forwardable"

class Foo
  def hello
    "Hello"
  end
  private :hello
end

class Bar
  extend Forwardable

  def_delegator :@foo, :hello

  def initialize
    @foo = Foo.new
  end

  def bar
    hello
  end
end

foo = Foo.new
foo.hello # => NoMethodError: private method `hello' called for #<Foo:0x007f57c3cd1c80>

bar = Bar.new
bar.bar # => "Hello"

I'm using Ruby 2.0.

役に立ちましたか?

解決

Not directly. If you're using Active Support core extensions 4.0 or newer, you can use Module#delegate instead:

require "active_support/core_ext/module/delegation"

class Foo
  def hello
    "Hello"
  end
  private :hello
end

class Bar
  delegate :hello, :to => :@foo

  def initialize
    @foo = Foo.new
  end

  def bar
    hello
  end
end
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top