Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用 IronRuby 解析 Yaml 文件,然后在 C# 中使用解析后的文档。这对于创建引擎 ( Ruby.CreateEngine()) 和执行YAML::load(File.open('myFile.yaml')).
Ruby.CreateEngine()
YAML::load(File.open('myFile.yaml'))
但是,这很好用,因为当我执行几行 ruby 代码时,我可以为文件名硬编码一个字符串。
现在,我想了解如何从 .Net 传递一个流以让 Yaml 解析器加载它。如何使用脚本引擎执行此操作?
您可以使用 ScriptScope 设置变量,然后从 Ruby 代码中使用它。例如:
ScriptEngine eng = Ruby.CreateEngine(); ScriptScope scope = eng.CreateScope(); scope.SetVariable("my_stream",stream); eng.Execute("self.my_stream.read() # or whatever...", scope);