Wadstk
Wadstk

Reputation: 185

Returning a hash from a subprocess in Ruby using IO.popen

I'm using IO.popen("cmd") in my Ruby script to run an Ironruby subroutine. In my Ironruby script, I am getting some data and storing it in a hash. In my Ruby script, I then use x=IO.popen("Iron ruby script") to retrieve the hash. Problem is, I can't seem to get the hash to show up in my Ruby script. I've tried x.gets, x.read, etc etc.

What would be the best way to get the hash from this pipe using IO.popen?

Thanks

Upvotes: 0

Views: 315

Answers (2)

steenslag
steenslag

Reputation: 80065

Ruby ships with dRuby, a distributed object system for Ruby. It may be overkill in this case. Demo:

#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"}

Upvotes: 0

Linuxios
Linuxios

Reputation: 35803

In the subproccess:

require "yaml"
puts YAML.dump(theHash)

In the parent:

require "yaml"
x=IO.popen(...)
theHash=YAML.load(x.read)

Upvotes: 1

Related Questions