when using system(), how do I redirect Exception msgs?

D

David E.

So from my console, I execute the Ruby script via:

ruby TestSuite.rb > /Users/dave/Desktop/outputtext.xml

Within my script I have several
---
begin
system('ruby /Users/dave/Desktop/testruby.rb');
system('ruby /Users/dave/Desktop/testruby2.rb');
system('ruby /Users/dave/Desktop/testruby3.rb');
rescue Exception=>msg
puts("Hi there exception")
puts(msg)
end
---

calls...

To test it, I put in testruby4.rb, but no file called testruby4.rb
exists.

I get a LoadError Exception as expected, and in my script I see the
trace on the console, but not via the puts() commands...

How come the script is ignoring the rescue clause I have here and how
can I get it to not ignore it?

THanks!
 
D

David E.

David E. wrote in post #964857:
So from my console, I execute the Ruby script via:

ruby TestSuite.rb > /Users/dave/Desktop/outputtext.xml

Within my script I have several
---
begin
system('ruby /Users/dave/Desktop/testruby.rb');
system('ruby /Users/dave/Desktop/testruby2.rb');
system('ruby /Users/dave/Desktop/testruby3.rb');
rescue Exception=>msg
puts("Hi there exception")
puts(msg)
end
---

calls...

To test it, I put in testruby4.rb, but no file called testruby4.rb
exists.

I get a LoadError Exception as expected, and in my script I see the
trace on the console, but not via the puts() commands...

How come the script is ignoring the rescue clause I have here and how
can I get it to not ignore it?

THanks!


I answered/found my own question.

Instead of system(), I use load(). This seems to run the script within
the "Calling script" context. I'll figure out more exactly why, but it
seems as if load() works.
 
G

Gary Wright

So from my console, I execute the Ruby script via:
=20
ruby TestSuite.rb > /Users/dave/Desktop/outputtext.xml
=20
Within my script I have several
---
begin
system('ruby /Users/dave/Desktop/testruby.rb');
system('ruby /Users/dave/Desktop/testruby2.rb');
system('ruby /Users/dave/Desktop/testruby3.rb');
rescue Exception=3D>msg
puts("Hi there exception")
puts(msg)
end
---
=20
calls...
=20
To test it, I put in testruby4.rb, but no file called testruby4.rb
exists.
=20
I get a LoadError Exception as expected, and in my script I see the
trace on the console, but not via the puts() commands...
=20
How come the script is ignoring the rescue clause I have here and how
can I get it to not ignore it?


The exception is occurring in the child ruby process started by 'system' =
and not in your TestSuite.rb ruby process. Ruby exceptions don't =
propagate from a child process to the parent. You need to capture the =
return value from 'system' to determine in the child process exited =
cleanly or not. If the process started by 'system' fails, 'system' will =
return false:

ruby-1.9.2-p0 > if system 'ruby foobar.rb'
ruby-1.9.2-p0 ?> puts 'ok'
ruby-1.9.2-p0 ?> else
ruby-1.9.2-p0 > puts 'failed'
ruby-1.9.2-p0 ?> end
ruby: No such file or directory -- foobar.rb (LoadError)
failed
=3D> nil=20


Gary Wright=
 
G

Gary Wright

I answered/found my own question.

Instead of system(), I use load(). This seems to run the script within
the "Calling script" context. I'll figure out more exactly why, but it
seems as if load() works.

Just be aware that you won't have very good isolation between your
scripts if you use load() since everything will be happening in the
context of a single Ruby process. That may or may not be OK depending
on what you are doing.

Gary Wright
 
D

David E.

You're right. Is there a better way to run the ruby script within a ruby
script and get the full output?
 
D

David E.

Ah... YOu're right. Is there a way to get all other values from the
system process? (such as the exception trace).
 
J

Jeremy Bopp

Ah... YOu're right. Is there a way to get all other values from the
system process? (such as the exception trace).

I think what you may want to do instead is continue using the load
method and set its optional second parameter to true for each call.
According to the documentation, that will run the file under an
anonymous module and protect your global namespace:

http://ruby-doc.org/core/classes/Kernel.html#M005940

-Jeremy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top