Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

A couple tricks that are specific to MRI and probably not useful for everyday programming:

1. Get the bytecode for some code:

    irb(main):001:0> x = RubyVM::InstructionSequence.new '1 + 1'
    => <RubyVM::InstructionSequence:<compiled>@<compiled>>
    irb(main):002:0> puts x.disasm
    == disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
    0000 trace            1                                               (   1)
    0002 putobject        1
    0004 putobject        1
    0006 opt_plus         <ic:1>
    0008 leave            
    => nil
    irb(main):003:0>
2. Output parser states and debugging information:

    $ ruby -y -e'1 + 1'
(The output is long so I haven't pasted it. :-) )


I've seen #1 before but never remember it so I want to include it. And #2.. that's one that's new to me. Yay for Ruby things :-)


With Rubinius, you can use the compile subcommand:

    $ bin/rbx compile -A -S -B -e '1 + 1'  # A(ST), S(exp), B(ytecode)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: