Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

awesome_printでRubyオブジェクトをさらに見やすくする(その3)

awesome_printはObject#methodsの使い勝手もグレードアップします。

例えばpでやった場合

"".methods
#=> [:<=>, :==, :===, :eql?, ...,  :__send__, :__id__]

ppでやっても

pp "".methods
#[:<=>,
# :==,
# :===,
#...,
# :__send__,
# :__id__]

と、大して手がかりになるものがありませんが…awesome_printなら

 ap ''.methods
#[
#    [  0]                               !()                       String (BasicObject)
#    [  1]                              !=(arg1)                   String (BasicObject)
#    [  2]                              !~(arg1)                   String (Kernel)
#    [  3]                               %(arg1)                   String
#...
#    [194]                        whiteish(*html)                  String
#    [195]                          yellow(*html)                  String
#    [196]                       yellowish(*html)                  String
#]

と、引数の数や大元のクラスまでわかる便利仕様。
さらに、返ってくる結果は今までと同様Arrayなので…

(''.methods - Object.methods).grep(/red/)
# [
#     [0]    red(*html) String
#     [1] redish(*html) String
# ]

といった感じでうろ覚えのメソッドを検索し、大雑把な使い勝手の推測にも役立ちます。