5
4

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.

PrettyBacktrace でデバッグを楽にする

Posted at

Backtrace を表示する際に、ローカル変数名や値も表示してくれるようにしてくれる Gem

使ってみる

README のサンプルをそのまま使う

sample.rb
def recursive n
  str = "Hi #{n}!!  " * 128
  if n > 0
    recursive n - 1
  else
    raise "bottom of recursive"
  end
end

recursive 3

これを実行するといつものエラー表示

pretty_backtrace.rb:10:in `recursive': bottom of recursive (RuntimeError)
	from pretty_backtrace.rb:8:in `recursive'
	from pretty_backtrace.rb:8:in `recursive'
	from pretty_backtrace.rb:8:in `recursive'
	from pretty_backtrace.rb:14:in `<main>'

PrettyBacktrace を有効にすると

sample.rb
require 'pretty_backtrace'
PrettyBacktrace.enable

def recursive n
  str = "Hi #{n}!!  " * 128
  if n > 0
    recursive n - 1
  else
    raise "bottom of recursive"
  end
end

recursive 3

こうなる

pretty_backtrace.rb:10:in `recursive' (n = 0, str = "Hi 0!!  Hi 0!!  Hi 0...): bottom of recursive (RuntimeError)
	from pretty_backtrace.rb:8:in `recursive' (n = 1, str = "Hi 1!!  Hi 1!!  Hi 1...)
	from pretty_backtrace.rb:8:in `recursive' (n = 2, str = "Hi 2!!  Hi 2!!  Hi 2...)
	from pretty_backtrace.rb:8:in `recursive' (n = 3, str = "Hi 3!!  Hi 3!!  Hi 3...)
	from pretty_backtrace.rb:14:in `<main>'

おー nstr の状態がよくわかる!
さらに multi line モードにすると

sample.rb
require 'pretty_backtrace'
PrettyBacktrace.enable
PrettyBacktrace.multi_line = true

...

Pretty!

pretty_backtrace.rb:10:in `recursive'
          [FILE]
             8|    recursive n - 1
             9|  else
        ->  10|    raise "bottom of recursive"
            11|  end
            12|end

          [LOCAL VARIABLES]
            n = 0
            str = "Hi 0!!  Hi 0!!  Hi 0!!  Hi 0!!  Hi 0!!  Hi 0!!  Hi 0!!  Hi 0...
: bottom of recursive (RuntimeError)
	from pretty_backtrace.rb:8:in `recursive'
          [FILE]
             6|  str = "Hi #{n}!!  " * 128
             7|  if n > 0
        ->   8|    recursive n - 1
             9|  else
            10|    raise "bottom of recursive"

          [LOCAL VARIABLES]
            n = 1
            str = "Hi 1!!  Hi 1!!  Hi 1!!  Hi 1!!  Hi 1!!  Hi 1!!  Hi 1!!  Hi 1...

	from pretty_backtrace.rb:8:in `recursive'
          [FILE]
             6|  str = "Hi #{n}!!  " * 128
             7|  if n > 0
        ->   8|    recursive n - 1
             9|  else
            10|    raise "bottom of recursive"

          [LOCAL VARIABLES]
            n = 2
            str = "Hi 2!!  Hi 2!!  Hi 2!!  Hi 2!!  Hi 2!!  Hi 2!!  Hi 2!!  Hi 2...

	from pretty_backtrace.rb:8:in `recursive'
          [FILE]
             6|  str = "Hi #{n}!!  " * 128
             7|  if n > 0
        ->   8|    recursive n - 1
             9|  else
            10|    raise "bottom of recursive"

          [LOCAL VARIABLES]
            n = 3
            str = "Hi 3!!  Hi 3!!  Hi 3!!  Hi 3!!  Hi 3!!  Hi 3!!  Hi 3!!  Hi 3...

	from pretty_backtrace.rb:14:in `<main>'
          [FILE]
            12|end
            13|
        ->  14|recursive 3

たった 3 行足すだけで、とっても便利!
いろいろな場面で重宝しそうですね。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?