Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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 コマンドっぽく表示させるシリーズ

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away