在Ruby中打印类的所有方法名称?

时间:2012-04-23 15:14:12

标签: ruby

如果我想列出类的所有方法,我可以使用Class_Name.methods,例如,列出FixNum类中的所有方法:

> 5.methods
 => [:to_s, :-@, :+, :-, :*, :/, :div, :%, :modulo, :divmod, :fdiv, :**, :abs, :magnitude, :==, :===, :<=>, :>, :>=, :<, :<=, :~, :&, :|, :^, :[], :<<, :>>, :to_f, :size, :zero?, :odd?, :even?, :succ, :integer?, :upto, :downto, :times, :next, :pred, :chr, :ord, :to_i, :to_int, :floor, :ceil, :truncate, :round, :gcd, :lcm, :gcdlcm, :numerator, :denominator, :to_r, :rationalize, :singleton_method_added, :coerce, :i, :+@, :eql?, :quo, :remainder, :real?, :nonzero?, :step, :to_c, :real, :imaginary, :imag, :abs2, :arg, :angle, :phase, :rectangular, :rect, :polar, :conjugate, :conj, :between?, :nil?, :=~, :!~, :hash, :class, :singleton_class, :clone, :dup, :initialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :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, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]

这个清单真的很难读。我也试过5.methods.sort,但这并没有让它更具可读性。

我经常在日常编程中使用方法列表。有没有办法漂亮地打印这个,所以它变得更容易阅读?

6 个答案:

答案 0 :(得分:33)

试试这个单行:

puts 5.methods.sort

答案 1 :(得分:14)

我喜欢使用awesome_print。作为奖励,它为您提供彩色输出,方法和它们的来源:

1.9.3-p125 :001 > require 'ap'
 => true 
1.9.3-p125 :002 > ap 5.methods; 0
[
    [  0]                          !()                 Fixnum (BasicObject)
    [  1]                         !=(arg1)             Fixnum (BasicObject)
    [  2]                         !~(arg1)             Fixnum (Kernel)
    [  3]                          %(arg1)             Fixnum
    [  4]                          &(arg1)             Fixnum
    [  5]                          *(arg1)             Fixnum
    [  6]                         **(arg1)             Fixnum
    [  7]                          +(arg1)             Fixnum
    [  8]                         +@()                 Fixnum (Numeric)
    [  9]                          -(arg1)             Fixnum
    [ 10]                         -@()                 Fixnum
    [ 11]                          /(arg1)             Fixnum
    [ 12]                          <(arg1)             Fixnum
    [ 13]                         <<(arg1)             Fixnum
    [ 14]                         <=(arg1)             Fixnum
    [ 15]                        <=>(arg1)             Fixnum
    [ 16]                         ==(arg1)             Fixnum
    [ 17]                        ===(arg1)             Fixnum
    [ 18]                         =~(arg1)             Fixnum (Kernel)
    [ 19]                          >(arg1)             Fixnum
    [ 20]                         >=(arg1)             Fixnum
    [ 21]                         >>(arg1)             Fixnum
    [ 22]                         [](arg1)             Fixnum
    [ 23]                          ^(arg1)             Fixnum
    [ 24]                     __id__()                 Fixnum (Kernel)
    [ 25]                   __send__(*arg1)            Fixnum (BasicObject)
    [ 26]                        abs()                 Fixnum
    [ 27]                       abs2()                 Fixnum (Numeric)
    [ 28]                         ai(*options)         Fixnum (Kernel)
    [ 29]                      angle()                 Fixnum (Numeric)
    [ 30]                        arg()                 Fixnum (Numeric)
    [ 31]            awesome_inspect(*options)         Fixnum (Kernel)
    [ 32]              awesome_print(object, *options) Fixnum (Kernel)
    [ 33]                   between?(arg1, arg2)       Fixnum (Comparable)
    [ 34]                       ceil()                 Fixnum (Integer)
    [ 35]                        chr(*arg1)            Fixnum (Integer)
    [ 36]                      class()                 Fixnum (Kernel)
    [ 37]                      clone()                 Fixnum (Kernel)
    [ 38]                     coerce(arg1)             Fixnum (Numeric)
    [ 39]                       conj()                 Fixnum (Numeric)
    [ 40]                  conjugate()                 Fixnum (Numeric)
    [ 41]    define_singleton_method(*arg1)            Fixnum (Kernel)
    [ 42]                denominator()                 Fixnum (Integer)
    [ 43]                    display(*arg1)            Fixnum (Kernel)
    [ 44]                        div(arg1)             Fixnum
    [ 45]                     divmod(arg1)             Fixnum
    [ 46]                     downto(arg1)             Fixnum (Integer)
    [ 47]                        dup()                 Fixnum (Kernel)
    [ 48]                   enum_for(*arg1)            Fixnum (Kernel)
    [ 49]                       eql?(arg1)             Fixnum (Numeric)
    [ 50]                     equal?(arg1)             Fixnum (BasicObject)
    [ 51]                      even?()                 Fixnum
    [ 52]                     extend(*arg1)            Fixnum (Kernel)
    [ 53]                       fdiv(arg1)             Fixnum
    [ 54]                      floor()                 Fixnum (Integer)
    [ 55]                     freeze()                 Fixnum (Kernel)
    [ 56]                    frozen?()                 Fixnum (Kernel)
    [ 57]                        gcd(arg1)             Fixnum (Integer)
    [ 58]                     gcdlcm(arg1)             Fixnum (Integer)
    [ 59]                       hash()                 Fixnum (Kernel)
    [ 60]                          i()                 Fixnum (Numeric)
    [ 61]                       imag()                 Fixnum (Numeric)
    [ 62]                  imaginary()                 Fixnum (Numeric)
    [ 63]           initialize_clone(arg1)             Fixnum (Kernel)
    [ 64]             initialize_dup(arg1)             Fixnum (Kernel)
    [ 65]                    inspect()                 Fixnum (Kernel)
    [ 66]              instance_eval(*arg1)            Fixnum (BasicObject)
    [ 67]              instance_exec(*arg1)            Fixnum (BasicObject)
    [ 68]               instance_of?(arg1)             Fixnum (Kernel)
    [ 69] instance_variable_defined?(arg1)             Fixnum (Kernel)
    [ 70]      instance_variable_get(arg1)             Fixnum (Kernel)
    [ 71]      instance_variable_set(arg1, arg2)       Fixnum (Kernel)
    [ 72]         instance_variables()                 Fixnum (Kernel)
    [ 73]                   integer?()                 Fixnum (Integer)
    [ 74]                      is_a?(arg1)             Fixnum (Kernel)
    [ 75]                   kind_of?(arg1)             Fixnum (Kernel)
    [ 76]                        lcm(arg1)             Fixnum (Integer)
    [ 77]                  magnitude()                 Fixnum
    [ 78]                     method(arg1)             Fixnum (Kernel)
    [ 79]                    methods(*args)            Fixnum (Object)
    [ 80]                     modulo(arg1)             Fixnum
    [ 81]                       next()                 Fixnum (Integer)
    [ 82]                       nil?()                 Fixnum (Kernel)
    [ 83]                   nonzero?()                 Fixnum (Numeric)
    [ 84]                  numerator()                 Fixnum (Integer)
    [ 85]                  object_id()                 Fixnum (Kernel)
    [ 86]                       odd?()                 Fixnum
    [ 87]                        ord()                 Fixnum (Integer)
    [ 88]                      phase()                 Fixnum (Numeric)
    [ 89]                      polar()                 Fixnum (Numeric)
    [ 90]                       pred()                 Fixnum (Integer)
    [ 91]            private_methods(*args)            Fixnum (Object)
    [ 92]          protected_methods(*args)            Fixnum (Object)
    [ 93]              public_method(arg1)             Fixnum (Kernel)
    [ 94]             public_methods(*args)            Fixnum (Object)
    [ 95]                public_send(*arg1)            Fixnum (Kernel)
    [ 96]                        quo(arg1)             Fixnum (Numeric)
    [ 97]                rationalize(*arg1)            Fixnum (Integer)
    [ 98]                       real()                 Fixnum (Numeric)
    [ 99]                      real?()                 Fixnum (Numeric)
    [100]                       rect()                 Fixnum (Numeric)
    [101]                rectangular()                 Fixnum (Numeric)
    [102]                  remainder(arg1)             Fixnum (Numeric)
    [103]                respond_to?(*arg1)            Fixnum (Kernel)
    [104]        respond_to_missing?(arg1, arg2)       Fixnum (Kernel)
    [105]                      round(*arg1)            Fixnum (Integer)
    [106]                       send(*arg1)            Fixnum (Kernel)
    [107]            singleton_class()                 Fixnum (Kernel)
    [108]     singleton_method_added(arg1)             Fixnum (Numeric)
    [109]          singleton_methods(*args)            Fixnum (Object)
    [110]                       size()                 Fixnum
    [111]                       step(*arg1)            Fixnum (Numeric)
    [112]                       succ()                 Fixnum
    [113]                      taint()                 Fixnum (Kernel)
    [114]                   tainted?()                 Fixnum (Kernel)
    [115]                        tap()                 Fixnum (Kernel)
    [116]                      times()                 Fixnum (Integer)
    [117]                       to_c()                 Fixnum (Numeric)
    [118]                    to_enum(*arg1)            Fixnum (Kernel)
    [119]                       to_f()                 Fixnum
    [120]                       to_i()                 Fixnum (Integer)
    [121]                     to_int()                 Fixnum (Integer)
    [122]                       to_r()                 Fixnum (Integer)
    [123]                       to_s(*arg1)            Fixnum
    [124]                   truncate()                 Fixnum (Integer)
    [125]                      trust()                 Fixnum (Kernel)
    [126]                    untaint()                 Fixnum (Kernel)
    [127]                    untrust()                 Fixnum (Kernel)
    [128]                 untrusted?()                 Fixnum (Kernel)
    [129]                       upto(arg1)             Fixnum (Integer)
    [130]                      zero?()                 Fixnum
    [131]                          |(arg1)             Fixnum
    [132]                          ~()                 Fixnum
]
 => 0

答案 2 :(得分:5)

排除从Object继承的方法可能也很有用。

(Fixnum.public_instance_methods - Object.public_instance_methods).sort
=> [:%, :&, :*, :**, :+, :+@, :-, :-@, :/, :<, :<<, :<=, :>, :>=, :>>, :[], :^, :abs, :abs2, :angle, :arg, :between?, :ceil, :chr, :coerce, :conj, :conjugate, :denominator, :div, :divmod, :downto, :even?, :fdiv, :floor, :gcd, :gcdlcm, :i, :imag, :imaginary, :integer?, :lcm, :magnitude, :modulo, :next, :nonzero?, :numerator, :odd?, :ord, :phase, :polar, :pred, :quo, :rationalize, :real, :real?, :rect, :rectangular, :remainder, :round, :singleton_method_added, :size, :step, :succ, :times, :to_c, :to_f, :to_i, :to_int, :to_r, :truncate, :upto, :zero?, :|, :~]

或者更具可读性:

(Fixnum.public_instance_methods - Object.public_instance_methods).sort.join(" ")
=> "% & * ** + +@ - -@ / < << <= > >= >> [] ^ abs abs2 angle arg between? ceil chr coerce conj conjugate denominator div divmod downto even? fdiv floor gcd gcdlcm i imag imaginary integer? lcm magnitude modulo next nonzero? numerator odd? ord phase polar pred quo rationalize real real? rect rectangular remainder round singleton_method_added size step succ times to_c to_f to_i to_int to_r truncate upto zero? | ~"

答案 3 :(得分:4)

几乎开箱即用的简单方法是:

require 'yaml'
y (5.methods - Object.methods).sort

为方便起见,您还可以创建包含这些方法的.irbrc文件:

require 'yaml'

def yppo(obj)
  y((obj.methods - Object.methods).sort)
end

def yppc(obj)
  y((obj.methods - Class.methods).sort)
end

然后就像yppo(5)一样简单。

我通常使用的另一个技巧是grep方法:

y (5.methods - Object.methods).sort.grep /to_/

答案 4 :(得分:4)

事实上,你回答了自己的问题:

require 'pp'
pp 5.methods

[:to_s,
 :-@,
 :+,
 :-,
 :*,
 :/,
 :div,
 :%,
 :modulo,
 :divmod,
 :fdiv,
 :**,
 :abs,
 :magnitude,
 :==,
 :===,
 :<=>,
etc

答案 5 :(得分:0)

您好,这是一个简单的答案

5.methods.each {|x| p x}

现在解释。 &#34; 5&#34;是&#34;数字&#34;并且可以是任何其他Interger ...... &#34;。方法&#34;这将调用适用于数字的所有方法的列表...... &#34; .each {| x | p x}&#34;这将在列表中打印所有方法。

我希望这会有所帮助。