28

如果我想列出我可以使用的类的所有方法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,但这并没有使它更具可读性。

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

4

6 回答 6

43

试试这个单行:

puts 5.methods.sort
于 2012-04-23T15:24:06.037 回答
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
于 2012-04-23T15:25:49.900 回答
6

排除继承自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? | ~"
于 2012-04-23T15:21:47.300 回答
4

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

require 'pp'
pp 5.methods

[:to_s,
 :-@,
 :+,
 :-,
 :*,
 :/,
 :div,
 :%,
 :modulo,
 :divmod,
 :fdiv,
 :**,
 :abs,
 :magnitude,
 :==,
 :===,
 :<=>,
etc
于 2012-04-23T15:25:19.147 回答
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_/
于 2012-04-23T15:25:04.463 回答
0

你好这是一个简单的答案

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

现在解释。“5”是“数字”,可以是任何其他整数......“.methods”这将调用适用于数字的所有方法的列表......“.each {|x| px}" 这将在列表中打印所有方法。

我希望这有帮助。

于 2017-01-08T22:51:14.820 回答