Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created January 22, 2023 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/465eb969ef1fb817e4ad493701b0020e to your computer and use it in GitHub Desktop.
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.
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