有没有办法只获得一个Ruby类';类方法

有没有办法只获得一个Ruby类';类方法,ruby,Ruby,我使用: 但是有没有一种方法可以直接获得类方法,比如: 1.9.3-p448 :057 > Object.methods => 270 1.9.3-p448 :058 > Object.instance_methods => 153 我知道我可以: 1.9.3-p448 :058 > Object.class_methods 但是有没有办法直接得到它们呢?另外,在后者中,除了类方法之外,还有什么可能的吗 thx对象.单例类.实例类方法对象.单例类.实例类方

我使用:

但是有没有一种方法可以直接获得类方法,比如:

1.9.3-p448 :057 > Object.methods
 => 270 
1.9.3-p448 :058 > Object.instance_methods
 => 153
我知道我可以:

1.9.3-p448 :058 > Object.class_methods
但是有没有办法直接得到它们呢?另外,在后者中,除了类方法之外,还有什么可能的吗


thx

对象.单例类.实例类方法
对象.单例类.实例类方法
klass.methods
给出类
klass
的类方法

1.9.3-p448 :057 > Object.methods - Object.instance_methods
Dir.methods
# => [:open, :foreach, :entries, :chdir, :getwd, :pwd, :chroot, :mkdir, :rmdir,
:delete, :unlink, :home, :glob, :[], :exist?, :exists?, :allocate, :new,
:superclass, :freeze, :===, :==, :<=>, :<, :<=, :>, :>=, :to_s, :inspect,
:included_modules, :include?, :name, :ancestors, :instance_methods,
:public_instance_methods, :protected_instance_methods, :private_instance_methods,
:constants, :const_get, :const_set, :const_defined?, :const_missing,
:class_variables, :remove_class_variable, :class_variable_get,
:class_variable_set, :class_variable_defined?, :public_constant,
:private_constant, :module_exec, :class_exec, :module_eval, :class_eval,
:method_defined?, :public_method_defined?, :private_method_defined?,
:protected_method_defined?, :public_class_method, :private_class_method,
:autoload, :autoload?, :instance_method, :public_instance_method, :nil?, :=~,
:!~, :eql?, :hash, :class, :singleton_class, :clone, :dup, :taint, :tainted?,
:untaint, :untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods,
:protected_methods, :private_methods, :public_methods, :instance_variables,
:instance_variable_get, :instance_variable_set, :instance_variable_defined?,
:remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send,
:public_send, :respond_to?, :extend, :display, :method, :public_method,
:define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=,
:instance_eval, :instance_exec, :__send__, :__id__]

klass.methods
为class
klass
提供类方法

1.9.3-p448 :057 > Object.methods - Object.instance_methods
Dir.methods
# => [:open, :foreach, :entries, :chdir, :getwd, :pwd, :chroot, :mkdir, :rmdir,
:delete, :unlink, :home, :glob, :[], :exist?, :exists?, :allocate, :new,
:superclass, :freeze, :===, :==, :<=>, :<, :<=, :>, :>=, :to_s, :inspect,
:included_modules, :include?, :name, :ancestors, :instance_methods,
:public_instance_methods, :protected_instance_methods, :private_instance_methods,
:constants, :const_get, :const_set, :const_defined?, :const_missing,
:class_variables, :remove_class_variable, :class_variable_get,
:class_variable_set, :class_variable_defined?, :public_constant,
:private_constant, :module_exec, :class_exec, :module_eval, :class_eval,
:method_defined?, :public_method_defined?, :private_method_defined?,
:protected_method_defined?, :public_class_method, :private_class_method,
:autoload, :autoload?, :instance_method, :public_instance_method, :nil?, :=~,
:!~, :eql?, :hash, :class, :singleton_class, :clone, :dup, :taint, :tainted?,
:untaint, :untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods,
:protected_methods, :private_methods, :public_methods, :instance_variables,
:instance_variable_get, :instance_variable_set, :instance_variable_defined?,
:remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send,
:public_send, :respond_to?, :extend, :display, :method, :public_method,
:define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=,
:instance_eval, :instance_exec, :__send__, :__id__]

这应该可以做到:

Dir.methods(false)
# => [:open, :foreach, :entries, :chdir, :getwd, :pwd, :chroot, :mkdir, :rmdir,
:delete, :unlink, :home, :glob, :[], :exist?, :exists?]

这应该可以做到:

Dir.methods(false)
# => [:open, :foreach, :entries, :chdir, :getwd, :pwd, :chroot, :mkdir, :rmdir,
:delete, :unlink, :home, :glob, :[], :exist?, :exists?]

可能
Object.class.instance\u方法
-与您的
Object.methods-Object.instance\u方法
不同,但可能是想要的结果。可能
Object.class.instance\u方法
-与您的
Object.methods-Object.instance\u方法
不同,但可能是想要的结果。@CarySwoveland No,这将为您提供对象类的任何实例方法。如果您想要给定对象的类方法,您可以访问它的单例类。但我们讨论的是大写字母“O”对象(即所有Ruby对象的默认根),以及object.singleton_class.instance_methods==object.class.instance_methods=>true。注意Object.class=>class和Object.singleton_class.祖先。first=>class。在看了其他答案后,我看到海报使用“Object”来指代一般对象。像“MyObject”这样的东西会更清晰。@CarySwoveland No,这将为您提供对象类的实例方法。如果您想要给定对象的类方法,您可以访问它的单例类。但我们讨论的是大写字母“O”对象(即所有Ruby对象的默认根),以及object.singleton_class.instance_methods==object.class.instance_methods=>true。注意Object.class=>class和Object.singleton_class.祖先。first=>class。在看了其他答案后,我看到海报使用“Object”来指代一般对象。类似“MyObject”的东西会更清晰。勤勉的解释:-)@BorisStitnicky我确实认为OP在寻找类方法,而不是实例方法<代码>方法会给你所有的。勤勉的解释:-)@BorisStitnicky我确实认为OP在寻找类方法,而不是实例方法<代码>方法将为您提供所有。