4
2

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.

コールスタックを tree コマンド風に表示する gem を作った

Posted at

method_call_tree というコールスタックを tree コマンドっぽく表示させる gem を作りました。

require 'method_call_tree'

class Foo
  def foo
    bar
    baz
  end

  def bar
  end

  def baz
    hoge
  end

  def hoge
  end
end

tree = MethodCallTree.create do
  Foo.new.foo
end

puts tree

結果

foo
├───── bar
└───── baz
        └───── hoge

クラス名も表示させたい場合は class オプションを使用します。

tree = MethodCallTree.create(class: true) do
  Foo.new.foo
end

結果

Foo::foo
├───── Foo::bar
└───── Foo::baz
        └───── Foo::hoge

args オプションを使用することで引数の情報も表示させることが出来ます。

require 'method_call_tree'

def fibonacci(a = 1, b = 0)
  return if a > 10
  fibonacci(a + b, a)
end

tree = MethodCallTree.create(args: true) do
  fibonacci
end

puts tree

結果

fibonacci(a = 1, b = 0)
└───── fibonacci(a = 1, b = 1)
        └───── fibonacci(a = 2, b = 1)
                └───── fibonacci(a = 3, b = 2)
                        └───── fibonacci(a = 5, b = 3)
                                └───── fibonacci(a = 8, b = 5)
                                        └───── fibonacci(a = 13, b = 8)

良さそう!

その他

他の tree コマンドっぽく表示させるシリーズ

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?