[英]Running two different interpreters on same script
我有一个用Ironruby编写的脚本,该脚本使用C#.dll检索哈希。 然后,我将在其余的Ruby代码中使用该哈希。 我宁愿不使用Ironruby解释器运行我的整个脚本。 无论如何,是否有通过IR解释器运行一段代码,获取哈希并通过常规Ruby解释器执行其余代码的情况?
谢谢
一种可能的解决方案是将脚本分成两部分,第一部分由铁红宝石执行,必须将其状态保存在yaml文件中,然后再将控制权交给第二部分,第二部分将由红宝石运行
这是一个小演示:
C:\devkit\home\demo>demo
"running program:demo_ir.rb"
"the first part of the script running by the iron_ruby interpreter"
"my_hash attributes:"
"attr1: first value"
"attr2: second value"
"attr3: 2012"
"hash_store_filename:temp.yaml"
"running program:demo_ruby.rb"
"hash_store_filename:temp.yaml"
"the second part of the script running by ruby 1.8.x interpreter"
"my_hash attributes:"
"attr1: first value"
"attr2: second value"
"attr3: 2012"
这是ironruby(demo_ir.rb)的第一部分的来源:
require "yaml"
p "running program:#{$0}"
hash_store_filename = ARGV[0]
my_hash = { attr1: 'first value', attr2: 'second value', attr3: 2012}
p "the first part of the script running by the iron_ruby interpreter"
p "my_hash attributes:"
p "attr1: #{my_hash[:attr1]}"
p "attr2: #{my_hash[:attr2]}"
p "attr3: #{my_hash[:attr3]}"
# save the state of the script in an array where my_hash is the first element
p "hash_store_filename:#{hash_store_filename}"
File.open( hash_store_filename, 'w' ) do |out|
YAML.dump( [my_hash], out )
end
这是ruby 1.8(demo_ruby.rb)的第二部分代码
require "yaml"
p "running program:#{$0}"
hash_store_filename = ARGV[0]
p "hash_store_filename:#{hash_store_filename}"
ar = YAML.load_file(hash_store_filename)
my_hash=ar[0]
p "the second part of the script running by ruby 1.8.x interpreter"
p "my_hash attributes:"
p "attr1: #{my_hash[:attr1]}"
p "attr2: #{my_hash[:attr2]}"
p "attr3: #{my_hash[:attr3]}"
和启动器:
@ECHO OFF
REM file: demo.bat
SET TEMP_YAML=temp.yaml
ir demo_ir.rb %TEMP_YAML%
ruby demo_ruby.rb %TEMP_YAML%
del %TEMP_YAML%
如果在并发环境中运行脚本,则可以在ironruby脚本中生成yaml文件的唯一临时名称,从而避免两个进程(或线程)尝试写入同一文件。
如果您愿意,可以使用一些C#代码而不是.bat来集成脚本的两个部分,但这有点困难(IMHO)
我成功使用以下方法测试了此解决方案:
C:\devkit\home\demo>ir -v
IronRuby 1.1.3.0 on .NET 4.0.30319.239
C:\devkit\home\demo>ruby -v
ruby 1.8.7 (2011-12-28 patchlevel 357) [i386-mingw32]
询问您是否需要澄清
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.