简体   繁体   English

Ruby BasicObject方法

[英]Ruby BasicObject methods

while going through Ruby, i encountered that BasicObject is the top most superclass of Class. 在浏览Ruby时,我遇到BasicObject是Class中最顶级的超类。 When I tried BasicObject.methods , i get list of these methods 当我尝试BasicObject.methods ,我得到了这些方法的列表

[:method_missing, :singleton_method_added, :singleton_method_undefined, :singleton_method_removed, :new, :become_java!, :allocate, :superclass, :java_class, :class_variables, :<=, :public_instance_methods, :prepend, :class_variable_get, :public_constant, :singleton_class?, :instance_methods, :freeze, :instance_method, :const_defined?, :to_s, :constants, :ancestors, :private_instance_methods, :===, :included_modules, :==, :using, :class_eval, :const_get, :refine, :protected_instance_methods, :public_instance_method, :class_variable_defined?, :inspect, :name, :private_constant, :<, :hash, :>, :>=, :module_exec, :protected_method_defined?, :module_eval, :const_missing, :class_exec, :const_set, :private_method_defined?, :public_class_method, :autoload, :<=>, :include, :public_method_defined?, :autoload?, :class_variable_set, :include?, :remove_class_variable, :deprecate_constant, :private_class_method, :method_defined?, :include_class, :handle_different_imports, :java_kind_of?, :public_send, :frozen?, :protected_methods, :java_implements, :public_method, :java, :singleton_methods, :untaint, :javafx, :enum_for, :private_methods, :method, :instance_variables, :object_id, :extend, :itself, :instance_variable_set, :respond_to?, :java_name, :methods, :to_java, :java_package, :singleton_class, :public_methods, :to_enum, :display, :tainted?, :instance_variable_defined?, :untrusted?, :define_singleton_method, :!~, :nil?, :com, :instance_of?, :java_require, :javax, :java_signature, :tap, :java_annotation, :send, :trust, :instance_variable_get, :is_a?, :eql?, :java_field, :remove_instance_variable, :untrust, :class, :=~, :org, :taint, :kind_of?, :clone, :dup, :!, :equal?, :instance_exec, :__id__, :instance_eval, :__send__, :!=]

(I have Jruby present as well so some extra methods) (我有Jruby以及一些额外的方法)

But when I call BasicObject.methods(false) (false parameter for displaying methods of the same class itself), I get 但是当我调用BasicObject.methods(false) (用于显示同一类本身的方法的false参数)时,我得到了

=> [:method_missing, :singleton_method_added, :singleton_method_undefined, :singleton_method_removed]

I have a query, ideally class.methods with false param should return only its methods and BasicObject.superclass returns nil. 我有一个查询,理想情况下,带有false param的class.methods应仅返回其方法,BasicObject.superclass返回nil。 Is there any other hierarchy? 还有其他层次结构吗? from where does these methods coming from. 从哪里来的这些方法。

I'm learning Ruby Apologozies if i'm missing something. 如果我错过了什么,我正在学习Ruby Apologozies。 Thanks 谢谢

Those methods are indeed defined on BasicObject, though they are mostly just empty definitions ( method_missing actually does some work). 这些方法确实是在BasicObject上定义的,尽管它们大多只是空定义( method_missing实际上做了一些工作)。 Check the source . 检查来源

I can't say exactly why, but there could be a hint in the comments: 我不能确切地说出原因,但评论中可能会有一些暗示:

Some care has been taken to make the implementation be as monomorphic as possible, so that the Java Hotspot engine can improve performance of it. 已经采取了一些措施使实现尽可能是单态的,以便Java Hotspot引擎可以提高它的性能。 That is the reason for several patterns that might seem odd in this class. 这就是几个模式在这个类中可能看起来很奇怪的原因。

Those methods are all common hooks that are redefined for metaprogramming purposes. 这些方法都是为元编程目的而重新定义的常见钩子。 By defining them on BasicObject, it could be that this allows for much faster method resolution by simplifying the inheritance chain. 通过在BasicObject上定义它们,可能会通过简化继承链来实现更快的方法解析。

You are not asking what instance methods are defined on BasicObject , that would be BasicObject.instance_methods . 您不是在询问BasicObject上定义了哪些实例方法,即BasicObject.instance_methods You are asking, what methods you can call on BasicObject . 您在询问,您可以在BasicObject调用哪些方法。 Well, BasicObject is an object just like any other object, so you can call any method on it that is defined in its class (which in this case is Class ) or in the superclasses of its class (which in this case are Module , Object , Kernel , and BasicObject ). 好吧, BasicObject就像任何其他对象一样是一个对象,所以你可以调用它上面定义的任何方法(在本例中是Class )或者在它的类的超类中(在本例中是ModuleObjectKernelBasicObject )。

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

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