Skip to content

Instantly share code, notes, and snippets.

@grahamlyons
Last active September 11, 2015 10:34
Show Gist options
  • Save grahamlyons/b8487540dc283b38b4f7 to your computer and use it in GitHub Desktop.
Save grahamlyons/b8487540dc283b38b4f7 to your computer and use it in GitHub Desktop.
Create two files with Ruby VM bytecode instructions
def get_test_class(a_assignment)
test_class = <<-EOF
class T
attr_accessor(:a)
def initialize
@a = 42
end
def t(this_a=nil)
if this_a
#{a_assignment}
end
a
end
end
EOF
end
iseq_without_assignment = RubyVM::InstructionSequence.compile(get_test_class(nil))
iseq_with_assignment = RubyVM::InstructionSequence.compile(get_test_class('a = this_a'))
File.open('iseq_without_assignment.txt', 'w+') do |f|
f.write(iseq_without_assignment.disassemble)
end
File.open('iseq_with_assignment.txt', 'w+') do |f|
f.write(iseq_with_assignment.disassemble)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment