Project

General

Profile

Actions

Bug #16107

closed

Module#refine and Module#using behaved unexpectedly

Added by UlyssesZhan (Ulysses Zhan) over 5 years ago. Updated over 5 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.6.0p0 (2018-12-25 revision 66547) [x64-mingw32]
[ruby-core:94382]

Description

The issue happened when I tried to run the code below:

# Main.rb
class A
end
class B
end

module M1
	refine A do
	  def foo *args
		  puts "foo"
	  end
	end
end

using M1
module M2
	def self.enable_foo
		refine B do
			def call_foo *args
				A.new.foo *args
			end
		end
	end
end

M2.enable_foo
using M2
B.new.call_foo

I expected it to output foo, but I got this:

Traceback (most recent call last):
        1: from Main.rb:28:in `<main>'
Main.rb:20:in `call_foo': undefined method `foo' for #<A:0x000000000719f3a0> (NoMethodError)
Did you mean?  for

I tried to put using M1 in M2 and its singleton class, but the same error was raised.
Maybe it is a Ruby bug. If it is not, I hope to get a workaround when I encounter similiar situations.
RUBY_VERSION is "2.6.0", and RUBY_PLATFORM is "x64-mingw32".

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

I shortened the example a little.

class A
end

module M1
  refine A do
    def foo
      puts "foo"
    end
  end
end

module M2
  using M1
  refine A do
    A.new.foo
  end
end

It seems that a refine block does not inherit outer usings.

Updated by osyo (manga osyo) over 5 years ago

(FYI) It works if you using M1 it in refine A .

class A
end

module M1
  refine A do
    def foo
      puts "foo"
    end
  end
end

module M2
  refine A do
    using M1

    # OK
    A.new.foo
  end
end

https://wandbox.org/permlink/P1vRSmsHAex7MpSL

Actions #4

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

  • Status changed from Open to Closed

Applied in changeset git|11a9f7ab9431b0f361e43b4ac2bd6ee44827d88b.


Search refinement module along nested usings

[Bug #16107]

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0