您可以将类中的模块包含在类中,以将类方法和实例方法添加到该特定类来扩展类功能.
module M def self.class_method_from_module 'from class_method_from_module' end def instance_method_from_module 'from instance_method_from_module' end end class C include M def self.class_method 'from class_method' end def instance_method 'from instance_method' end end puts C.class_method => 'from class_method' puts C.class_method_from_module => 'from class_method_from_module' puts C.new.instance_method => 'from instance_method' puts C.new.instance_method_from_module => 'instance_method_from_module'
现在即使从存储器中删除模块M,具体如下:
Object.send(:remove_const,:M) #remove the module M puts C.class_method_from_module => 'from class_method_from_module' puts C.new.instance_method_from_module => 'instance_method_from_module'
而不是缺少方法.这是为什么?什么是删除模块添加到类的功能的最佳方法?
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。