Rubinius のおもしろ機能: 構文木とバイトコードを表示する
たとえば次のような Ruby スクリプト (hoge.rb) があったとする。
def hello(name)
puts "Hello #{name}!"
end
hello('World')
ここで「rubinius describe hoge.rb」を実行すると、hoge.rb の構文木とバイトコードが表示される。「rbx describe hoge.rb」だとうまくいかないので、「cd rubinius.code; ln -s $PWD/shotgun/rubinius $HOME/bin」とかを実行しておくこと。
$ rubinius describe hoge.rb
Sexp:
[:block,
[:newline,
1,
"hoge2.rb",
[:defn,
:hello,
[:scope,
[:block,
[:args, [:name], [], nil, nil],
[:newline,
2,
"hoge2.rb",
[:fcall,
:puts,
[:array,
[:dstr, "Hello ", [:evstr, [:lvar, :name, 0]], [:str, "!"]]]]]],
[:name]]]],
[:newline, 5, "hoge2.rb", [:fcall, :hello, [:array, [:str, "World"]]]]]
Bytecode:
0000: push_literal #<CompiledMethod:0x40081 name=hello file=hoge2.rb>
0002: push_literal :hello
0004: push_self
0005: send_stack :__add_method__, 2
0008: pop
0009: push_literal "World"
0011: string_dup
0012: push_self
0013: set_call_flags 1
0015: send_stack :hello, 1
0018: pop
0019: push_true
0020: sret
ステキすぎる!
参考になりそうな記事: