4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pry-rails+hirb+hirb-unicode-steakknifeを動かす為のアレコレ

Posted at

環境

  • rails 5.2.2
  • ruby 2.5.5

Gemfileと.pryrc

Gemfile
group :development, :test do
  # なんか色々〜
  gem 'pry-rails'
  gem 'pry-byebug'
  gem 'pry-doc'
  gem 'hirb'
  # 本家hirb-unicodeは依存のunicode-display_widthが古く他のgemと相性が悪い
  gem 'hirb-unicode-steakknife'
end
.pryrc
begin
  require 'hirb'
  # これが無いとdisplay_widthが無くてエラー
  require 'unicode/display_width/string_ext'
  # これもrequireしないと勝手には反応しない
  require 'hirb-unicode'
rescue LoadError
  # hirbがrequireできないでござる
end

if defined? Hirb
  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をオフにしたい時はHirb.disableする
  Hirb.enable
end

こんな感じ

参考

https://ruby-rails.hatenadiary.com/entry/20141024/1414160189
https://github.com/steakknife/hirb-unicode/blob/master/lib/hirb/unicode/string_util.rb#L5
https://github.com/janlelis/unicode-display_width#usage-with-string-extension

最後に

もっといい書き方あったら教えてちょ

4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?