我是Ruby脚本语言的新手。
我正在学习如何在Ruby中生成字节码。我找到了生成字节码的答案。
但我不知道如何运行生成的字节码。我在网上搜索了一下,但没有找到答案。
生成一个字节码:-
puts RubyVM::InstructionSequence.compile("x = 50; x > 100 ? 'foo' : 'bar'").disassemble
输出为,
== disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1] s1)
[ 2] x
0000 trace 1 ( 1)
0002 putobject 50
0004 setlocal x
0006 trace 1
0008 getlocal x
0010 putobject 100
0012 opt_gt <ic:1>
0014 branchunless 20
0016 putstring "foo"
0018 leave
0019 pop
0020 putstring "bar"
0022 leave
我不知道如何通过使用生成的字节码来执行相同的脚本。
有谁能告诉我如何执行这个。
提前感谢!
2条答案
按热度按时间nkkqxpd91#
**TL;DR;**您正在查找
.eval
方法。.compile
方法将返回RubyVM::InstructionSequence
类的一个示例,该类具有.eval
方法,用于评估/运行您的“编译”指令。或者,一个oneliner:
58wvjzkj2#
RBC
hello.rb
运行
请注意,对于多个文件,