Ruby1.9的隐藏参数"--dump"

前几天,我的同事老高在看Ruby1.9.2源码的时候,发现Ruby1.9有一个隐藏参数:"--dump"(注意是两个减号). 下面具体说说这个参数:

 

1. --dump 参数

--dump 可以打印出ruby及指定脚本的相关信息,具体如下:

[version, copyright, usage, yydebug, syntax, parsetree , parsetree_with_comment, insns ]

其中有几项的内容我们已经熟悉,例如:version,copyright,usage. 不过这次我们感兴趣的是parsetree和insns的内容。

 

parsetree -- 打印指定脚本的Parse Tree

insns        -- 打印指定脚本的RubyVM(YARV)指令

 

2. 具体用法举例

假设有一个已经写好的脚本1.rb,存放在C盘。我们可以如下使用"--dump"参数:

ruby --dump parsetree c:\1.rb

 

3.测试结果

测试脚本:

class A
  def hello
    puts 'Hello'
  end
end

A.new.hello

打印RubyVM指令:ruby --dump insns c:\1.rb

输出

== disasm: <RubyVM::InstructionSequence:<main>@c:/1.rb>=================
0000 trace            1                                               (   1)
0002 putspecialobject 3
0004 putnil
0005 defineclass      :A, <class:A>, 0
0009 pop
0010 trace            1                                               (   7)
0012 getinlinecache   19, <ic:0>
0015 getconstant      :A
0017 setinlinecache   <ic:0>
0019 send             :new, 0, nil, 0, <ic:1>
0025 send             :hello, 0, nil, 0, <ic:2>
0031 leave
== disasm: <RubyVM::InstructionSequence:<class:A>@c:/1.rb>==============
0000 trace            2                                               (   1)
0002 trace            1                                               (   2)
0004 putspecialobject 1
0006 putspecialobject 2
0008 putobject        :hello
0010 putiseq          hello
0012 send             :"core#define_method", 3, nil, 0, <ic:0>
0018 trace            4                                               (   5)
0020 leave                                                            (   2)
== disasm: <RubyVM::InstructionSequence:hello@c:/1.rb>==================
0000 trace            8                                               (   2)
0002 trace            1                                               (   3)
0004 putnil
0005 putstring        "Hello"
0007 send             :puts, 1, nil, 8, <ic:0>
0013 trace            16                                              (   4)
0015 leave                                                            (   3)
 

 

打印ParseTree:ruby --dump parsetree c:\1.rb

输出:

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

# @ NODE_SCOPE (line: 7)
# +- nd_tbl: (empty)
# +- nd_args:
# |   (null node)
# +- nd_body:
#     @ NODE_BLOCK (line: 1)
#     +- nd_head:
#     |   @ NODE_CLASS (line: 1)
#     |   +- nd_cpath:
#     |   |   @ NODE_COLON2 (line: 1)
#     |   |   +- nd_mid: :A
#     |   |   +- nd_head:
#     |   |       (null node)
#     |   +- nd_super:
#     |   |   (null node)
#     |   +- nd_body:
#     |       @ NODE_SCOPE (line: 5)
#     |       +- nd_tbl: (empty)
#     |       +- nd_args:
#     |       |   (null node)
#     |       +- nd_body:
#     |           @ NODE_DEFN (line: 2)
#     |           +- nd_mid: :hello
#     |           +- nd_defn:
#     |               @ NODE_SCOPE (line: 4)
#     |               +- nd_tbl: (empty)
#     |               +- nd_args:
#     |               |   @ NODE_ARGS (line: 2)
#     |               |   +- nd_frml: 0
#     |               |   +- nd_next:
#     |               |   |   @ NODE_ARGS_AUX (line: 2)
#     |               |   |   +- nd_rest: (null)
#     |               |   |   +- nd_body: (null)
#     |               |   |   +- nd_next:
#     |               |   |       (null node)
#     |               |   +- nd_opt:
#     |               |       (null node)
#     |               +- nd_body:
#     |                   @ NODE_FCALL (line: 3)
#     |                   +- nd_mid: :puts
#     |                   +- nd_args:
#     |                       @ NODE_ARRAY (line: 3)
#     |                       +- nd_alen: 1
#     |                       +- nd_head:
#     |                       |   @ NODE_STR (line: 3)
#     |                       |   +- nd_lit: "Hello"
#     |                       +- nd_next:
#     |                           (null node)
#     +- nd_next:
#         @ NODE_BLOCK (line: 7)
#         +- nd_head:
#         |   @ NODE_CALL (line: 7)
#         |   +- nd_mid: :hello
#         |   +- nd_recv:
#         |   |   @ NODE_CALL (line: 7)
#         |   |   +- nd_mid: :new
#         |   |   +- nd_recv:
#         |   |   |   @ NODE_CONST (line: 7)
#         |   |   |   +- nd_vid: :A
#         |   |   +- nd_args:
#         |   |       (null node)
#         |   +- nd_args:
#         |       (null node)
#         +- nd_next:
#             (null node)
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值