Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > capturing warnings

Reply
Thread Tools

capturing warnings

 
 
Chris Pine
Guest
Posts: n/a
 
      02-12-2004
Hello,

Here's a method which executes the code in the string `code', giving it the
input from the array of strings `input' as it asks for it. The output is
captured, and so are any errors.

However, it doesn't capture warnings. Anyone know how to do that? NOTE: I
only want to capture warning generated by the code in `code', not in the
rest of my program.



def executeCode (code, input)
# Wrap code to catch errors and to stop SystemExit.
code = <<-END_CODE
begin
#{code}
rescue SystemExit
rescue Exception => error
puts error.inspect
end
END_CODE

strIO = StringIO.new

if !input.empty?
input = input.join("\n")+"\n"
input = StringIO.new(input, "r")
class << strIO; self; end.module_eval do
['gets', 'getc', 'read'].each do |meth|
define_method(meth) do |*params|
inStr = input.method(meth).call(*params)
puts @@INPUT+inStr.chomp+(@@INPUT.reverse) # Echo input.
inStr
end
end
end
end

# Pass these methods to strIO:
kernelMethods = ['puts', 'putc', 'gets']

# Swap out Kernel methods...
kernelMethods.each do |meth|
Kernel.module_eval "alias __temp__tutorial__#{meth}__ #{meth}"
Kernel.module_eval do
define_method(meth) do |*params|
strIO.method(meth).call(*params)
end
end
end

begin
strIO.instance_eval code
rescue Exception => error # Catch parse errors.
return error.inspect
end

# ...and swap them back in.
kernelMethods.each do |meth|
Kernel.module_eval "alias #{meth} __temp__tutorial__#{meth}__"
end

strIO.string
end



Any help would be much appreciated,

Chris



 
Reply With Quote
 
 
 
 
Simon Strandgaard
Guest
Posts: n/a
 
      02-12-2004
On Thu, 12 Feb 2004 14:21:05 +0000, Chris Pine wrote:
> Here's a method which executes the code in the string `code', giving it the
> input from the array of strings `input' as it asks for it. The output is
> captured, and so are any errors.

[snip]

open3 should do it. Its distributed with Ruby.

However if you on windows you may check
http://raa.ruby-lang.org/list.rhtml?name=win32_popen

--
Simon Strandgaard




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with syntesis warnings JnCodesigns VHDL 2 04-30-2007 06:31 PM
there are too many warnings (you are about to....etc etc forever) after installing Firefox trevor_smithson@yahoo.com Firefox 2 10-13-2005 07:35 PM
What to do with "Unconnected output port" warnings? Herb T VHDL 1 04-04-2005 09:24 AM
use warnings; and use Warnings; give different results Ted Sung Perl Misc 1 08-30-2004 10:22 PM
disabling certain warnings in synopsys dc Tuukka Toivonen VHDL 1 05-11-2004 01:51 PM



Advertisments