Edited at

RubyのソースのASTを見る方法

More than 5 years have passed since last update.


問題

cout/ruby-internalが新しめのrubyで動かない


--dump=parsetree オプションを使う

https://bugs.ruby-lang.org/issues/2455 で本体にnodeをダンプするオプションが入っているので、

ruby hoge.rb --dump=parsetreeなりruby -e "hoge" --dump=parsetreeなりすると見れる


example

ruby -e "1+2+3" --dump=parsetree

###########################################################
## Do NOT use this node dump for any purpose other than ##
## debug and research. Compatibility is not guaranteed. ##
###########################################################

# @ NODE_SCOPE (line: 1)
# +- nd_tbl: (empty)
# +- nd_args:
# | (null node)
# +- nd_body:
# @ NODE_CALL (line: 1)
# +- nd_mid: :+
# +- nd_recv:
# | @ NODE_CALL (line: 1)
# | +- nd_mid: :+
# | +- nd_recv:
# | | @ NODE_LIT (line: 1)
# | | +- nd_lit: 1
# | +- nd_args:
# | @ NODE_ARRAY (line: 1)
# | +- nd_alen: 1
# | +- nd_head:
# | | @ NODE_LIT (line: 1)
# | | +- nd_lit: 2
# | +- nd_next:
# | (null node)
# +- nd_args:
# @ NODE_ARRAY (line: 1)
# +- nd_alen: 1
# +- nd_head:
# | @ NODE_LIT (line: 1)
# | +- nd_lit: 3
# +- nd_next:
# (null node)

何かの役にたつといいですね