Question

I have a script written in Ironruby that uses a C# .dll to retrieve a hash. I then use that hash throughout the rest of my Ruby code. I would rather not run my entire script off of the Ironruby interpreter. Is there anyway to run a section of code on the IR interpreter, get the hash, and execute the rest of the code via the regular Ruby interpreter?

Thanks

Was it helpful?

Solution

One possible solution is to split up the script into two parts, the first part executed by iron ruby has to save his state in a yaml file before handing control to the second part which will run by ruby

here a small demo:

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"

here the source of the first part for 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

here the code of the second part for 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]}"

and the launcher:

@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%

if you run the script in a concurrent environment you can generate a unique temporary name of the yaml file in the ironruby script avoiding that two process ( or thread ) try to write the same file.

If you prefer you could use some C# line of code, instead of a .bat, to integrate the two parts of the script, but this is a is a bit more difficult (IMHO)

I successfully test this solution using:

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]

ask if you need some clarification

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top