Ruby中是否有一种方法可以打印出对象的公共方法 [英] Is there a way in Ruby to print out the public methods of an Object

查看:500
本文介绍了Ruby中是否有一种方法可以打印出对象的公共方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

...不包括通用Object的所有公共方法?我的意思是,除了进行数组减法.我只想快速查看一下某个对象对我可用的内容,而无需查看文档.

... without including all of the public methods of the generic Object? I mean, other than doing array subtraction. I just want to quickly review what's available to me from an object sometimes without going to the docs.

推荐答案

methodsinstance_methodspublic_methodsprivate_methodsprotected_methods都接受布尔参数,以确定对象的父对象的方法是否包括在内.

methods, instance_methods, public_methods, private_methods and protected_methods all accept a boolean parameter to determine if the methods of your object's parents are included.

例如:

ruby-1.9.2-p0 > class MyClass < Object; def my_method; return true; end; end;
ruby-1.9.2-p0 > MyClass.new.public_methods
 => [:my_method, :nil?, :===, :=~, :!~, :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup, :initialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :to_s, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?, :extend, :display, :method, :public_method, :define_singleton_method, :__id__, :object_id, :to_enum, :enum_for, :==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__] 
ruby-1.9.2-p0 > MyClass.new.public_methods(false)
 => [:my_method]

如@Marnen所指出的那样,动态定义的方法(例如,使用method_missing)不会在此处显示.您唯一的选择就是希望您正在使用的库有据可查.

As noted by @Marnen, the methods defined dynamically (eg. with method_missing) won't appear here. Your only bet for these ones is hoping that the libraries you're using are well documented.

这篇关于Ruby中是否有一种方法可以打印出对象的公共方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆