Project

General

Profile

Actions

Bug #15591

closed

SimpleDelegator: Undefined method when delegating a refined method

Bug #15591: SimpleDelegator: Undefined method when delegating a refined method

Added by bryanp (Bryan Powell) over 6 years ago. Updated over 6 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:91439]

Description

SimpleDelegator does not seem compatible with refinements (tested on 2.5.3 and 2.6.0).

Here's an example:

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:

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.

Updated by bryanp (Bryan Powell) over 6 years ago Actions #1 [ruby-core:91440]

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.

Updated by matz (Yukihiro Matsumoto) over 6 years ago Actions #2

  • Status changed from Open to Closed
Actions

Also available in: PDF Atom