LoginSignup
24
22

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-07-09

問題

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)

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

24
22
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
24
22