January 2024
Intermediate to advanced
718 pages
20h 15m
English
If you’d like to know what Ruby is doing with all that code you’re writing, you can ask the Ruby interpreter to show you the intermediate code that it’s executing.
You can ask it to compile the Ruby code in a string or in a file and then disassemble it and even run it. You might wonder if it can dump the opcodes out and later reload them. The answer is no—the interpreter has the code to do this, but it’s disabled because there isn’t yet an intermediate code verifier for the Ruby interpreter.
Here’s a trivial example of disassembly:
| | code = RubyVM::InstructionSequence.compile('a = 1; puts 1 + a') |
| | puts code.disassemble |
Produces:
| | == disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(1,17)> |
| | local table (size: ... |
Read now
Unlock full access