Yes, there is a way.
First you need to make the InstructionSequence method available to the load , which is disabled by default:
require 'fiddle' class RubyVM::InstructionSequence
Since the RubyVM::InstructionSequence.load method can load compiled VM instructions as an array, you can freely use this for serialization (de) purposes:
irb> # compile simple ruby program into its instruction sequence irb> seq = RubyVM::InstructionSequence.new <<-EOS irb: p 'Hello, world !' irb: EOS => <RubyVM::InstructionSequence:<compiled>@<compiled> irb> # serialize sequence as Array instance representation irb> data = Marshal.dump seq.to_a => "\x04\b[\x13\"-YARVInstructionSequence/SimpleDataFormat … ]" irb> # de-serialize previously serialized sequence irb> seq_loaded = Marshal.load data => ["YARVInstructionSequence/SimpleDataFormat", 2, 2, 1, { … ] irb> # load deserialized Array back into instruction sequence irb> new_iseq = RubyVM::InstructionSequence.load seq_loaded => <RubyVM::InstructionSequence:<compiled>@<compiled>> irb> # execute instruction sequence in current context irb> new_iseq.eval "Hello, world !" => "Hello, world !"
What are all people;)
source share