LoginSignup
8
6

More than 5 years have passed since last update.

pryを使う

Last updated at Posted at 2015-04-28

WorkInProgress

ref

install

  • 本体
    • gem install pry
  • pry-doc
    • gem install pry-doc
  • windowsで色の設定とか
    • gem install pry-theme

で、コマンドプロンプトとかでpryとして起動すると下記のようになっていると思う

[1] pry(main)> 

よく使ってる機能

ls

変数とメソッドの一覧を表示
ちょっとどうだったかな?という時に便利

# 現在のスコープの変数とメソッドの一覧
[1] pry(main)> ls
self.methods: inspect  to_s
locals: _  __  _dir_  _ex_  _file_  _in_  _out_  _pry_
# 指定したオブジェクトの変数とメソッドの一覧
[2] pry(main)> ls 1
Comparable#methods: between?
Numeric#methods:
  +@     arg     conjugate  imag       phase         pretty_print_cycle  real?        remainder               to_c
  abs2   coerce  eql?       imaginary  polar         quo                 rect         singleton_method_added
  angle  conj    i          nonzero?   pretty_print  real                rectangular  step
Integer#methods:
  ceil  denominator  floor  gcdlcm    lcm   numerator  pred         round  to_i    to_r      upto
  chr   downto       gcd    integer?  next  ord        rationalize  times  to_int  truncate
Fixnum#methods:
  %  *   +  -@  <   <=   ==   >   >>  ^    bit_length  divmod  fdiv     magnitude  odd?  succ  to_s   |
  &  **  -  /   <<  <=>  ===  >=  []  abs  div         even?   inspect  modulo     size  to_f  zero?  ~

が、クラス名とかが青色で見にくくて色の設定を変更した
pryでlsした際のオブジェクト名の色を変更する

cd

Move into a new context (object or scope).
指定したオブジェクトの中に入る感じ

[16] pry(main)> ls
self.methods: inspect  to_s
locals: _  __  _dir_  _ex_  _file_  _in_  _out_  _pry_

# 1(つまりFixnum)の中に移動する
[17] pry(main)> cd 1

# 移動後のスコープはFixnumなので、lsだけで下記の結果となる
[18] pry(1):1> ls
Comparable#methods: between?
Numeric#methods:
  +@    angle  coerce  conjugate  i     imaginary  phase  pretty_print        quo   real?  rectangular  singleton_method_added  to_c
  abs2  arg    conj    eql?       imag  nonzero?   polar  pretty_print_cycle  real  rect   remainder    step
Integer#methods:
  ceil  denominator  floor  gcdlcm    lcm   numerator  pred         round  to_i    to_r      upto
  chr   downto       gcd    integer?  next  ord        rationalize  times  to_int  truncate
Fixnum#methods:
  %  *   +  -@  <   <=   ==   >   >>  ^        abs         div     even?  inspect    modulo  size  to_f  zero?  ~
  &  **  -  /   <<  <=>  ===  >=  []  __pry__  bit_length  divmod  fdiv   magnitude  odd?    succ  to_s  |
locals: _  __  _dir_  _ex_  _file_  _in_  _out_  _pry_

?

show-sourceのエイリアス(別名)
詳細は後述のpry-docの項にて

$

show-docのエイリアス(別名)
詳細は後述のpry-docの項にて

binding.pry

デバッグに
上記のようなコマンドではなく、実行したいソースの中に仕込む
RubyistならデバッグにはPryのbinding.pryがおすすめ

何がいいの?

すぐ実行して試せる点

#to_sを試してみる

[1] pry(main)> 1.to_s
=> "1"

連番付与した名称が欲しいな、とか

[1] pry(main)> (1..10).each { |i| puts "col_name#{'%02d' % i}" }
col_name01
col_name02
col_name03
col_name04
col_name05
col_name06
col_name07
col_name08
col_name09
col_name10
=> 1..10

pry-doc

pry-docでカジュアルにRubyのソースコードを読む

pryの中でソースとか使い方とか見たりする
gem install pry-doc

例:to_sのリファレンスを見る

? 1.to_s

[1] pry(main)> ? 1.to_s

From: numeric.c (C Method):
Owner: Fixnum
Visibility: public
Signature: to_s(*arg1)
Number of lines: 9

Returns a string containing the representation of fix radix base
(between 2 and 36).

   12345.to_s       #=> "12345"
   12345.to_s(2)    #=> "11000000111001"
   12345.to_s(8)    #=> "30071"
   12345.to_s(10)   #=> "12345"
   12345.to_s(16)   #=> "3039"
   12345.to_s(36)   #=> "9ix"

例:to_sのソースを見る

$ 1.to_s

[1] pry(main)> $ 1.to_s

From: numeric.c (C Method):
Owner: Fixnum
Visibility: public
Number of lines: 15

static VALUE
fix_to_s(int argc, VALUE *argv, VALUE x)
{
    int base;

    if (argc == 0) base = 10;
    else {
        VALUE b;

        rb_scan_args(argc, argv, "01", &b);
        base = NUM2INT(b);
    }

    return rb_fix2str(x, base);
}
8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6