Question

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

Was it helpful?

Solution

Do as below :

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

class Bar
  include Foo
end

Bar.new.foo # => Foo
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top