Rolando Abarca wrote:
> On Oct 19, 2007, at 11:45 AM, Koos Brandt wrote:
> 
>> OK, that works and thanks a lot.
>>
>> But I actually still have the program in memory, ie string object.  
>> I can
>> most certaily write it to a tempfile and do the above, but I would  
>> have
>> like
>> system("ruby #{contents}") or system("irb #{contents}")
>> Tried both, and they just go into a long process!
> 
> if you have the contents in a string, you could always open a piped
> shell:
> 
> pipe = IO.popen("ruby", "w")
> pipe.write contents
> pipe.close
> 
> regards,

Thanks Rolando

Found the Session project while searching for help on popen. Installed 
and tried the examples.

I'm using Mac OSX 104.10

Here is what I am actually trying to do. The first part, and where I'm 
actually struggling is similar to Textmate run as ruby file

So I have a ruby script (editor) that will use wxRuby and will have a 
ruby script inside a variable, say @content

This script will be a typical wxRuby program (devprog), and might have 
some puts in it for tracing etc.

I want to execute the wxRuby program (devprog), and be able to see its 
puts (stdout) results while it is running.

I've tried various approaches, but all seem to rely on the devprog 
exiting before the puts results come back.

In my editor, I would have a textbox, similar to a typical IDE, to 
display the puts results.

Here is the typical wxRuby program (devprog)

require 'rubygems'
require 'wx'
include Wx
require 'wx_sugar/all'


class ExampleApp < App
  def on_init
    self.app_name = File.basename(__FILE__) +"(" + RUBY_VERSION + ")"
    frame = Frame.new( nil, :title => app_name()  )
      btn = Button.new(frame,:label => "Check")
      evt_button(btn.get_id()) {|event| on_create(event)}
      frame.show()
  end
  def on_create(event)
      puts "checked" + event.get_string()
  end
  def on_exit
    puts "thank you, and goodbye"
  end
end

ExampleApp.new.main_loop()

it writes checked everytime the button is pressed.

I start this program from my editor using the following for now:
#approach 1
@content = IO.read(File.dirname(__FILE__)+ "/TestSources/devprog.rb")
pipe = IO.popen("ruby", "w")
pipe.write @content
pipe.close

puts "end of program"

and also

#approach 2
require "open3"
include Open3
@content = IO.read(File.dirname(__FILE__)+ "/TestSources/devprog.rb")
stdin, stdout, stderr = popen3("ruby")
stdin.write @content

puts "end of program"

Approach 1 returns the output soon as I close the GUI app.
Approach 2 does not give me the results back, it writes "end of program" 
immediately to the Rubymate Stdout, even before the GUI is finally 
displayed.

I am still a newbie to Ruby so be gentle here. All the popen and popen3 
seems to be the stuff I should use, or maybe fork, but I just don't know 
enough about them to get what I want to do. The stuff I used here is 
sort of what I could understand from other peoples postings, includig 
your already supplied and much appreciated help.

I think in approach 1 the problem is that I need to close the write 
part, and then open a read part. I've tried a couple of ideas there, 
using w+ and a+, none of which worked. I get the idea it has something 
to do with how one overlaods the stdout, stderr, but most of what I 
could find on those had an issue with the $S_ status, which again is 
concerned with the results after the program closes.

In approach 2 I sort of have the issue that: "I think I need some form 
of waiting or callback mechanism that can be informed that some output 
has been received, and that we can retrieve it and display it". When I 
tried the Approach 2 I sort of expected that it already will link to 
local (editor) stdout, but it is clearly not the case. Would there be a 
way to just connect the two streams?

I studied documents from Edgar Toernig 
http://article.gmane.org/gmane.comp.lang.lua.general/42157 which sort 
suggest that the bidirectional communciation would be tricky, but then 
popen3 seem to address just that. Also a few others, like 
http://pleac.sourceforge.net/pleac_ruby/processmanagementetc.html and 
also the session project of ara.t.howard, but I don't want to just try 
ideas without some basic understanding of where I'm aiming.

Thanks for the help so far, and I would appreciate any further help.
-- 
Posted via http://www.ruby-forum.com/.