Bye Bye Moore

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

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

awesome_printはrails consoleでも活用できます。

下準備

Gemfileのdevelopmentグループに、pry-railsとawesome_printを記述

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'

  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'

  gem 'pry-rails'          #<=NEW
  gem "awesome_print"  #<=NEW
end

~/.pryrcのrailsconsole用設定を追加

if defined? Rails::Console
  require "awesome_print"
  AwesomePrint.pry!
end

あとは、通常通りrails cを起動です。

$ bundle install
$ rails c

一度設定をしてあげれば、読み込み時に毎度毎度awesome_printを読みに行かずに済みます。

[1] pry(main)>  [(1..9).to_a,['42']], limit: 3
#[
#    [0] [
#        [0] 1,
#        [1] .. [7],
#        [8] 9
#    ],
#    [1] [
#        [0] "42"
#    ]
#]