irb で実行結果を pp で出力する

irb で実行結果を pp で出力する手段がいくつかあるのでまとめ。

irb の起動オプションで変更する

irb の起動オプションに --inspect pp を追加することで pp の出力になります。

$ irb --inspect pp
irb(main):001:0> (1..10).to_h { |it| [it, it] }
 = > {1=>1,
 2=>2,
 3=>3,
 4=>4,
 5=>5,
 6=>6,
 7=>7,
 8=>8,
 9=>9,
 10=>10}

irb の起動中に変更する

conf.inspect_mode = :pp を実行することで以降の出力が pp になります。

$ irb
irb(main):001:0> conf.inspect_mode = :pp
irb(main):002:0> (1..10).to_h { |it| [it, it] }
 = > {1=>1,
 2=>2,
 3=>3,
 4=>4,
 5=>5,
 6=>6,
 7=>7,
 8=>8,
 9=>9,
 10=>10}

.irbrc で設定する場合

.irbrc ファイルで制御する場合、以下の設定を追加することで pp で出力できます。

# .irbrc
IRB.conf[:INSPECT_MODE] = :pp

pp 以外にも yamlmarshal 形式で出力することもできるので気になる人は調べてみるといいと思います。

参照