سؤال

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