ruby – 类<< self中的类定义是什么?
发布时间:2020-12-16 22:09:26 所属栏目:百科 来源:网络整理
导读:我知道如何添加类方法和类行为 using self class (eigenclass).但是,当阅读 some source code时,我看到另一个用法: class LetterAvatar class self class Identity end endend 这个怎么用?它有什么作用,什么时候应该使用它?什么是(可能更认可)替代方式来
我知道如何添加类方法和类行为
using
self << class (eigenclass).但是,当阅读
some source code时,我看到另一个用法:
class LetterAvatar class << self class Identity end end end 这个怎么用?它有什么作用,什么时候应该使用它?什么是(可能更认可)替代方式来写这个? 解决方法
我认为他们这样做是因为他们不需要这个班.
没有打开单例类,流程将如下所示(假设在原代码中的元类中定义的每个方法都将以self为前缀). 他们可以将身份定义为 class LetterAvatar class Identity end end 然后使用self.generate方法中的类如下: class LetterAvatar # code omitted def self.generate identity = LetterAvatar::Identity.from_username(username) # code omitted end # other class level methods defined with `self.` end 但是为什么在单例类(在生成中)中仅使用Identity类(而不需要在别的地方访问)就这样做? 解决方案是IMO非常优雅,以前没有看到过这样的东西. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |