Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created July 30, 2015 02:35
Show Gist options
  • Save JoshCheek/a8e9dbb6f54fd1a69216 to your computer and use it in GitHub Desktop.
Save JoshCheek/a8e9dbb6f54fd1a69216 to your computer and use it in GitHub Desktop.
Seeing a compiled method
# Classes store a hash of methods, the keys being the method names... but what are the values?
# Here you can see an example of what the code looks like when compiled.
# It's not important to understand how these instructions allow Ruby to work,
# but I want you to see that there is a concrete thing there that you can look at.
# (note, all of the `trace` instructions are irrelevant and can be ignored,
# their purpose is to allow you to hook into events from the outside http://www.rubydoc.info/stdlib/core/TracePoint)
def greet
puts "hello world"
end
puts RubyVM::InstructionSequence.disasm method(:greet)
# >> == disasm: <RubyVM::InstructionSequence:greet@/var/folders/7g/mbft22555w3_2nqs_h1kbglw0000gn/T/seeing_is_believing_temp_dir20150729-32981-tz4p8a/program.rb>
# >> 0000 trace 8 ( 1)
# >> 0002 trace 1 ( 2)
# >> 0004 putself
# >> 0005 putstring "hello world"
# >> 0007 opt_send_simple <callinfo!mid:puts, argc:1, FCALL|ARGS_SKIP>
# >> 0009 trace 16 ( 3)
# >> 0011 leave ( 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment