문제

I have simple code

module Foo
  def foo
    p self
    #p 'Foo' -> bad decision for me
  end
end

class Bar
  include Foo
end

Bar.new.foo #=> #<Bar:0x00000002f0faf8> 

But I need something like this

Bar.new.foo #=> Foo

I need the name of the module from which this method was called. So, what are the ways to find out the name of the module

도움이 되었습니까?

해결책

Do as below :

module Foo
  def foo
    method(__method__).owner
  end
end

class Bar
  include Foo
end

Bar.new.foo # => Foo
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top