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