Ruby相当于python的imp.load_compiled

时间:2015-02-17 05:40:10

标签: python ruby

我正在寻找以下python代码中的编译代码加载的ruby实现:

global tuser, temail
tuser = **imp.load_compiled**('tuser', uplug)
temail = **imp.load_compiled**('temail', eplug)

感谢。

1 个答案:

答案 0 :(得分:0)

使用RubyVM::InstructionSequencehttps://ruby-doc.org/core-2.6/RubyVM/InstructionSequence.html

示例: https://devtechnica.com/ruby-language/compile-ruby-code-to-binary-and-execute-it

# compiler.rb
source_code = File.open(ARGV[0], 'r')       # load the file from arguments
compiled_code = RubyVM::InstructionSequence.compile(source_code)
binary_code = compiled_code.to_binary
binary_file = File.open(ARGV[0].split('.').first + '.bin', 'w+')
binary_file.puts binary_code
binary_file.close

# exec.rb
executable_binary_code = File.open(ARGV[0], 'r').readlines.join('')
compiled_instruction_sequence = \
  RubyVM::InstructionSequence.load_from_binary(executable_binary_code)
compiled_instruction_sequence.eval
ruby compiler.rb fibonacci.rb
ruby exec.rb fibonacci.bin