Eval alternative to execute string

I have the following stored in a file Example1.rb
require ‘rubygems’
require ‘wx’
include Wx
require ‘wx_sugar/all’

class ExampleApp < App
def on_init
frame = Frame.new( nil, :title => “Example1.rb” ).show
end
end

ExampleApp.new.main_loop()

If I run the following snippet, it works
mycontents = IO.read(File.dirname(FILE)+ “/testSources/Example1.rb”)
eval mycontents

if I however create a class and do the eval from inside the class, I get
an error that ‘include’ is not a method of Document.

class Document
def initialize()
@content = “” # this is the actual content of the
document
end

eval the text content

def run
eval @content
end

load the file from disk and put it into @content

def load
@content = IO.read(File.dirname(FILE)+
“/testSources/Example1.rb”))
puts @content
end

end

myProg = Document.new()
myProg.load
myProg.run

If I however include in the class file the requires and includes, and
comment them out in Example1.rb, then it also works.

require ‘rubygems’
require ‘wx’
include Wx
require ‘wx_sugar/all’

The story about eval I take note of, but I think it is clear what I want
to to here: read a file and execute it. From what I see above, this is
not entirely what eval will do for me. Eval makes the source file part
of the current executing environment.

What I actually want to do is: ruby “big piece of text” and that should
not be part of my current environment if you understand what I mean.

On Oct 19, 2007, at 11:16 AM, Koos B. wrote:

What I actually want to do is: ruby “big piece of text” and that
should
not be part of my current environment if you understand what I mean.

If you want nothing to do with the external ruby file, maybe you
should do something like this:

system(“ruby #{the_external_file}”)

regards,

On Oct 19, 9:16 am, Koos B. [email protected] wrote:

end
class Document
def load

not entirely what eval will do for me. Eval makes the source file part
of the current executing environment.

What I actually want to do is: ruby “big piece of text” and that should
not be part of my current environment if you understand what I mean.

Posted viahttp://www.ruby-forum.com/.

Does it have to be a string? Since you don’t want it to be part of
your current environment, wouldn’t something like this work just as
well:

ruby testSources/Example1.rb

On Oct 19, 2007, at 11:45 AM, Koos B. 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,

Rolando A. wrote:

On Oct 19, 2007, at 11:16 AM, Koos B. wrote:

What I actually want to do is: ruby �big piece of text� and that
should
not be part of my current environment if you understand what I mean.

If you want nothing to do with the external ruby file, maybe you
should do something like this:

system(“ruby #{the_external_file}”)

regards,

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!

Rolando A. wrote:

On Oct 19, 2007, at 11:45 AM, Koos B. 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.

Hi –

On 22/10/2007, Koos B. [email protected] wrote:

http://pleac.sourceforge.net/pleac_ruby/processmanagementetc.html and

As a quick aside, I’d ignore anything in PLEAC as far as Ruby goes.
It’s not very good, and is clearly just a Perl -> Ruby copy. Yuck.

– Thomas A.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs