From: "yermej@..." Date: 2007-10-19T23:40:04+09:00 Subject: Re: Eval alternative to execute string On Oct 19, 9:16 am, Koos Brandt wrote: > 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. > -- > 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`