Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 方法中的模块名_Ruby_Metaprogramming - Fatal编程技术网

Ruby 方法中的模块名

Ruby 方法中的模块名,ruby,metaprogramming,Ruby,Metaprogramming,我有简单的代码 module Foo def foo p self #p 'Foo' -> bad decision for me end end class Bar include Foo end Bar.new.foo #=> #<Bar:0x00000002f0faf8> 我需要调用此方法的模块的名称。 那么,找出模块名称的方法是什么呢?请按以下步骤操作: module Foo def foo method(__met

我有简单的代码

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

class Bar
  include Foo
end

Bar.new.foo #=> #<Bar:0x00000002f0faf8> 
我需要调用此方法的模块的名称。 那么,找出模块名称的方法是什么呢?请按以下步骤操作:

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

class Bar
  include Foo
end

Bar.new.foo # => Foo

元编程在哪里?@Monk_Code这个定义有点争议:-)@Monk_Code当然,如果可能的话,这只是一些元编程决策。谢谢,正是我需要的
module Foo
  def foo
    method(__method__).owner
  end
end

class Bar
  include Foo
end

Bar.new.foo # => Foo