简体   繁体   English

稍后定义的类上的模块实例方法

[英]Module instance methods on later defined class

I want to note that this is COMPLETELY a made up question. 我想指出,这完全是一个问题。 I am aware that there are other ways to accomplish this. 我知道还有其他方法可以实现这一目标。

I want to declare a module like so 我想声明一个这样的模块

module Foo
 # some logic here to
 # get instance method 'foo' on
 # a later defined class
end

then later I want to declare a class like: 然后我想申报一个类:

class Foo::Bar
end

Then WITHOUT using include or extend be able to do this: 然后没有使用include或extend能够这样做:

Foo::Bar.new.foo

and have it call the foo method I defined in module Foo 并让它调用我在模块Foo中定义的foo方法

module Foo
 class Bar
   def foo
     puts "erik is a dummy"
     end
   end
 end
Foo::Bar.new.foo
=> erik is a dummy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM