【问题标题】:Is there a way in Ruby to print out the public methods of an ObjectRuby中有没有办法打印出对象的公共方法
【发布时间】:2011-12-29 14:42:38
【问题描述】:

...不包括通用对象的所有公共方法?我的意思是,除了做数组减法。我只是想快速查看对象中可用的内容,有时无需查看文档。

【问题讨论】:

    标签: ruby reflection public-method


    【解决方案1】:

    我开始尝试在https://github.com/bf4/Notes/blob/master/code/ruby_inspection.rb 中的某一时刻记录所有这些检查方法

    如其他答案所述:

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

    首先,我喜欢对方法进行排序

    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
    

    有用的技巧 Grep 找出你的选择是什么

    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"]
    

    另见https://stackoverflow.com/questions/123494/whats-your-favourite-irb-trick

    【讨论】:

      【解决方案2】:

      例如:

      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)不会出现在这里。对于这些,您唯一的选择是希望您使用的库是有据可查的。

      【讨论】:

      • 也许您应该将示例更改为 ''.public_methods[].public_methods 之类的东西。任何了解 Ruby 的人都清楚,您的示例列出了 Array 类对象 自身 响应的方法,而不是 Array 类的实例方法,但是这可能会误导新手。
      • @JörgWMittag 感谢您的意见。我选择了一个自定义类,因为 Array 或 String 实例有太多方法,并且 SO 代码块不换行。目前尚不清楚结果是否相同。
      【解决方案3】:

      这是您要寻找的结果吗?

      class Foo
        def bar
          p "bar"
        end
      end
      
      p Foo.public_instance_methods(false) # => [:bar]
      


      ps 我想这不是你想要的结果:

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

      【讨论】:

        【解决方案4】:

        如果有,它也不会非常有用:通常,公共方法不是您唯一的选择,因为 Ruby 能够通过动态元编程来伪造方法。所以你不能真的依赖instance_methods 告诉你很多有用的东西。

        【讨论】:

          猜你喜欢
          • 2010-09-14
          • 1970-01-01
          • 1970-01-01
          • 2011-05-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多