[ruby-core:91440] [Ruby trunk Bug#15591] SimpleDelegator: Undefined method when delegating a refined method

From: bryan@...
Date: 2019-02-06 19:04:15 UTC
List: ruby-core #91440
Issue #15591 has been updated by bryanp (Bryan Powell).


Apologies--I reported this too quickly. After looking at the implementation of `Delegator` I realized the above code should refine `BasicObject` rather than `Object`. Making that change allows both examples to work as expected. I believe this can be closed as a non-issue.

Bryan P.

----------------------------------------
Bug #15591: SimpleDelegator: Undefined method when delegating a refined method
https://bugs.ruby-lang.org/issues/15591#change-76698

* Author: bryanp (Bryan Powell)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
SimpleDelegator does not seem compatible with refinements (tested on 2.5.3 and 2.6.0).

Here's an example:

``` ruby
require "delegate"

module SuperDuper
  refine Object do
    def super_duper
      puts "!"
    end
  end
end

class Result < SimpleDelegator; end

using SuperDuper

value = ""

# Works fine.
#
value.super_duper
# => !

# Breaks unexpectedly :(
#
Result.new(value).super_duper
# => undefined method `super_duper' for "":Result (NoMethodError)
```

Creating my own naive delegator works as expected:

```ruby
require "delegate"

module SuperDuper
  refine Object do
    def super_duper
      puts "!"
    end
  end
end

class Result
  def initialize(value)
    @value = value
  end

  def method_missing(method, *args, &block)
    if @value.respond_to?(method)
      @value.public_send(method, *args, &block)
    else
      super
    end
  end
end

using SuperDuper

value = ""

# Works fine.
#
value.super_duper
# => !

# Works fine.
#
Result.new(value).super_duper
# => !
```

Happy to work on fixing this, but first wanted to confirm that this is not expected behavior.

Bryan P.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next