Is there a way in Ruby to print out the public methods of an Object

时光毁灭记忆、已成空白 提交于 2019-12-11 00:09:46

问题


... 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.


回答1:


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.

For example:

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]

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.




回答2:


Is this the result you were looking for?

class Foo
  def bar
    p "bar"
  end
end

p Foo.public_instance_methods(false) # => [:bar]


ps I expect this was not the result you were after:

p Foo.public_methods(false)          # => [:allocate, :new, :superclass]



回答3:


If there were, it wouldn't be terribly useful: often, the public methods are not your only options, due to Ruby's ability to fake methods by dynamic metaprogramming. So you can't really rely on instance_methods to tell you much that's useful.




回答4:


I started to try to document all these inspection methods at one point in https://github.com/bf4/Notes/blob/master/code/ruby_inspection.rb

As noted in other answers:

class Foo; def bar; end; def self.baz; end; end

Firstly, I like to sort the methods

Foo.public_methods.sort # all public instance methods
Foo.public_methods(false).sort # public class methods defined in the class
Foo.new.public_methods.sort # all public instance methods
Foo.new.public_methods(false).sort # public instance methods defined in the class

Useful tip Grep to find out what your options are

Foo.public_methods.sort.grep /methods/ # all public class methods matching /method/
# ["instance_methods", "methods", "private_instance_methods", "private_methods", "protected_instance_methods", "protected_methods", "public_instance_methods", "public_methods", "singleton_methods"]
Foo.new.public_methods.sort.grep /methods/
#  ["methods", "private_methods", "protected_methods", "public_methods", "singleton_methods"]

Also see https://stackoverflow.com/questions/123494/whats-your-favourite-irb-trick



来源:https://stackoverflow.com/questions/8138304/is-there-a-way-in-ruby-to-print-out-the-public-methods-of-an-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!