LoginSignup
34
29

More than 5 years have passed since last update.

Pryをより快適にしてくれるGemsとその設定方法

Last updated at Posted at 2015-09-03

概要

Ruby の 標準 REPL は irb ですが、その代わりに pry を使っている人も多いかと思います。

何も設定をしなくても、pry は 標準の irb よりも快適に使用することができます。

しかし、いくつかの Gem と組み合わせることでより快適になります。

おすすめ Gems

どちらの Gem も出力が非常に読みやすくなって、開発効率があがります!

Awsome Print は常に入れておいても損はないと思います。

Awsome Print の設定

Awsome Print は明示的に有効化する必要があるので、.pryrcで常に有効化しておくと快適です。

.pryrc
begin
  require "awesome_print"
  Pry.config.print = proc { |output, value| output.puts value.ai }
rescue LoadError
  puts "no awesome_print :("
end

Hirb の設定

Hirb も同じく、明示的に有効化する必要があります。

.pryrc
begin
  require "hirb"
rescue LoadError
  puts "no hirb :("
end

if defined? Hirb
  # Slightly dirty hack to fully support in-session Hirb.disable/enable toggling
  Hirb::View.instance_eval do
    def enable_output_method
      @output_method = true
      @old_print = Pry.config.print
      Pry.config.print = proc do |*args|
        Hirb::View.view_or_page_output(args[1]) || @old_print.call(*args)
      end
    end

    def disable_output_method
      Pry.config.print = @old_print
      @output_method = nil
    end
  end

  Hirb.enable
end

付録

私が今現在 Pry 関係で導入している Gem 一覧と.pryrcです。

Gem 一覧

私が Gemfile に書いている pry 関係の Gem は以下のとおりです。

Gemfile
gem_group :development, :test do
  gem "pry-coolline"
  gem "pry-rails"
  gem "pry-byebug"
  gem "pry-stack_explorer"

  gem "awesome_print"

  gem "hirb"
  gem "hirb-unicode"
end

pry 設定

pry には.pryrcという設定ファイルがあり、一部の Gem を常に有効化するためにはここに設定を書く必要があります。

私の.pryrcは以下のとおりです。

.pryrc
# awesome_print
begin
  require "awesome_print"
  Pry.config.print = proc { |output, value| output.puts value.ai }
rescue LoadError
  puts "no awesome_print :("
end

# hirb
begin
  require "hirb"
rescue LoadError
  puts "no hirb :("
end

if defined? Hirb
  # Slightly dirty hack to fully support in-session Hirb.disable/enable toggling
  Hirb::View.instance_eval do
    def enable_output_method
      @output_method = true
      @old_print = Pry.config.print
      Pry.config.print = proc do |*args|
        Hirb::View.view_or_page_output(args[1]) || @old_print.call(*args)
      end
    end

    def disable_output_method
      Pry.config.print = @old_print
      @output_method = nil
    end
  end

  Hirb.enable
end

necojackarc/dotfiles/pryrc - Github

快適!

謝辞

@scivola さんに誤字修正をしていただきました。

ありがとうございます。

34
29
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
34
29