Project

General

Profile

Actions

Bug #15278

closed

Refinements don't work well with Forwardable

Added by chucke (Tiago Cardoso) over 5 years ago. Updated over 5 years ago.

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

Description

Refined methods become unreachable when called through def_delegator.

require "forwardable"

module URIExtensions
  refine URI::Generic do
    def authority
      port_string = port == default_port ? nil : ":#{port}"
      "#{host}#{port_string}"
    end

    def origin
      "#{scheme}://#{authority}"
    end
  end
end

using URIExtensions

class Container
  extend Forwardable
  def_delegator :@uri, :origin
  def_delegator :@uri, :authority

  def initialize(uri)
    @uri = uri
  end
end


u = URI("https://google.com")
container = Container.new(u)
puts container.origin
puts container.authority

Updated by matz (Yukihiro Matsumoto) over 5 years ago

  • Status changed from Open to Closed

It's intentional. Refinement is lexical overriding of methods. That means refined methods (e.g. origin in this case) are not called from delegators.

Matz.

Actions

Also available in: Atom PDF

Like0
Like0