pryでshow-resourceを使い、クラスやモジュール、メソッドの定義を確認する


以下が手順

pryはあらかじめインストールしておく

pryを起動

$ pry

Arrayクラスへcdで入る

[1] pry(main)> cd Array

Arrayクラスで使用可能なメソッド一覧をlsで表示

[2] pry(Array):1> ls
Array.methods: []  try_convert
Array#methods: 
  &              concat      hash                product               slice     
  *              count       include?            push                  slice!    
  +              cycle       index               rassoc                sort      
  -              delete      insert              reject                sort!     
  <<             delete_at   inspect             reject!               sort_by!  
  <=>            delete_if   join                repeated_combination  sum       
  ==             difference  keep_if             repeated_permutation  take      
  []             dig         last                replace               take_while
  []=            drop        length              reverse               to_a      
  all?           drop_while  map                 reverse!              to_ary    
  any?           each        map!                reverse_each          to_h      
  append         each_index  max                 rindex                to_s      
  assoc          empty?      min                 rotate                transpose 
  at             eql?        none?               rotate!               union     
  bsearch        fetch       one?                sample                uniq      
  bsearch_index  fill        pack                select                uniq!     
  clear          filter      permutation         select!               unshift   
  collect        filter!     place               shelljoin             values_at 
  collect!       find_index  pop                 shift                 zip       
  combination    first       prepend             shuffle               |         
  compact        flatten     pretty_print        shuffle!            
  compact!       flatten!    pretty_print_cycle  size                
locals: _  __  _dir_  _ex_  _file_  _in_  _out_  _pry_

show-source [メソッド名]でメソッドの詳細を確認できるが、gemのpry-docがインストールされていないと表示されない(c言語で書かれたメソッドであるため)

[3] pry(Array):1> show-source to_a

From: array.c (C Method):
Owner: Array
Visibility: public
Number of lines: 10

static VALUE
rb_ary_to_a(VALUE ary)
{
    if (rb_obj_class(ary) != rb_cArray) {
        VALUE dup = rb_ary_new2(RARRAY_LEN(ary));
        rb_ary_replace(dup, ary);
        return dup;
    }
    return ary;
}