0

我在我的 Ruby 脚本中使用 IO.popen("cmd") 来运行 Ironruby 子例程。在我的 Ironruby 脚本中,我获取了一些数据并将其存储在哈希中。在我的 Ruby 脚本中,我使用 x=IO.popen("Iron ruby​​ script") 来检索哈希。问题是,我似乎无法让哈希显示在我的 Ruby 脚本中。我试过 x.gets、x.read 等。

使用 IO.popen 从该管道获取哈希的最佳方法是什么?

谢谢

4

2 回答 2

1

在子过程中:

require "yaml"
puts YAML.dump(theHash)

在父级中:

require "yaml"
x=IO.popen(...)
theHash=YAML.load(x.read)
于 2012-05-24T19:10:30.323 回答
0

Ruby 附带 dRuby,一个用于 Ruby 的分布式对象系统。在这种情况下,这可能是矫枉过正。演示:

#test1.rb
require 'drb/drb' # in standard lib

the_hash = Hash[:key1,'value1',:key2, 'value2']
DRb.start_service("druby://localhost:8787", the_hash)
DRb.thread.join


#test2.rb
require 'drb/drb'

DRb.start_service
p DRbObject.new_with_uri("druby://localhost:8787")
#=>{:key1=>"value1", :key2=>"value2"}
于 2012-05-24T19:34:48.600 回答