當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Ruby InstructionSequence.compile用法及代碼示例

本文簡要介紹ruby語言中 RubyVM::InstructionSequence.compile 的用法。

用法

compile(source[, file[, path[, line[, options]]]]) → iseq
new(source[, file[, path[, line[, options]]]]) → iseq

獲取 source ,一個 Ruby 代碼的 String 並將其編譯為 InstructionSequence

可選地采用 filepathline ,它們說明了 source 中 ruby 代碼的文件路徑、真實路徑和第一行號,它們是附加到返回的 iseq 的元數據。

file 用於“__FILE__”和異常回溯。 path 用於require_relative 基礎。建議這些應該是相同的完整路徑。

options 可以是 truefalseHash ,用於修改 Ruby iseq 編譯器的默認行為。

有關有效編譯選項的詳細信息,請參閱 ::compile_option=

RubyVM::InstructionSequence.compile("a = 1 + 2")
#=> <RubyVM::InstructionSequence:<compiled>@<compiled>>

path = "test.rb"
RubyVM::InstructionSequence.compile(File.read(path), path, File.expand_path(path))
#=> <RubyVM::InstructionSequence:<compiled>@test.rb:1>

path = File.expand_path("test.rb")
RubyVM::InstructionSequence.compile(File.read(path), path, path)
#=> <RubyVM::InstructionSequence:<compiled>@/absolute/path/to/test.rb:1>

相關用法


注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 InstructionSequence.compile。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。