Created
January 22, 2023 18:00
-
-
Save havenwood/465eb969ef1fb817e4ad493701b0020e to your computer and use it in GitHub Desktop.
A quick example for #ruby IRC of yomikomu-like compiling Ruby to YARB IR bytecode, caching it to disk, then loading and evaling it later.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'zlib' | |
path = 'example.rb' | |
## | |
# Compile and cache Ruby binary to disk. | |
binary = RubyVM::InstructionSequence.compile_file(path).to_binary | |
binary_path = "#{path}.yarb.gz" | |
Zlib::GzipWriter.open(binary_path) do | |
_1.write(binary) | |
end | |
## | |
# Load and eval cached a Ruby compiled binary. | |
Zlib::GzipReader.open(binary_path) do | |
RubyVM::InstructionSequence.load_from_binary(_1.read).eval | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment