# Eval alternative to execute string

**URL:** <https://www.ruby-forum.com/t/eval-alternative-to-execute-string/117709>\
**Category:** Ruby\
**Created:** [October 19, 2007, 4:16pm UTC](https://www.ruby-forum.com/t/eval-alternative-to-execute-string/117709 "2007-10-19T16:16:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![koos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2acd7d/32.png) [@koos](https://www.ruby-forum.com/u/koos)\
**Post date:** [October 19, 2007, 4:16pm UTC](https://www.ruby-forum.com/t/eval-alternative-to-execute-string/117709/1 "2007-10-19T16:16:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![koos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2acd7d/32.png) [@koos](https://www.ruby-forum.com/u/koos)\
**Post date:** [October 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/eval-alternative-to-execute-string/117709/2 "2007-10-19T16:30:07Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![koos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2acd7d/32.png) [@koos](https://www.ruby-forum.com/u/koos)\
**Post date:** [October 19, 2007, 4:40pm UTC](https://www.ruby-forum.com/t/eval-alternative-to-execute-string/117709/3 "2007-10-19T16:40:56Z")

</div>

On Oct 19, 9:16 am, Koos B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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`

---

<div class="post-metadata">

**Author:** ![koos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2acd7d/32.png) [@koos](https://www.ruby-forum.com/u/koos)\
**Post date:** [October 19, 2007, 4:59pm UTC](https://www.ruby-forum.com/t/eval-alternative-to-execute-string/117709/4 "2007-10-19T16:59:11Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![koos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2acd7d/32.png) [@koos](https://www.ruby-forum.com/u/koos)\
**Post date:** [October 19, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/eval-alternative-to-execute-string/117709/5 "2007-10-19T16:45:44Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![koos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2acd7d/32.png) [@koos](https://www.ruby-forum.com/u/koos)\
**Post date:** [October 22, 2007, 8:18am UTC](https://www.ruby-forum.com/t/eval-alternative-to-execute-string/117709/6 "2007-10-22T08:18:03Z")

</div>

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](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  
[Process Management and Communication](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.

---

<div class="post-metadata">

**Author:** ![koos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2acd7d/32.png) [@koos](https://www.ruby-forum.com/u/koos)\
**Post date:** [October 22, 2007, 8:30am UTC](https://www.ruby-forum.com/t/eval-alternative-to-execute-string/117709/7 "2007-10-22T08:30:13Z")

</div>

Hi –

On 22/10/2007, Koos B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [Process Management and Communication](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.
