capturing warnings

Discussion in 'Ruby' started by Chris Pine, Feb 12, 2004.

  1. Chris Pine

    Chris Pine Guest

    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
     
    Chris Pine, Feb 12, 2004
    #1
    1. Advertising

  2. 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
     
    Simon Strandgaard, Feb 12, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles Shannon Hendrix

    REGEX: capturing on optional groups which fail

    Charles Shannon Hendrix, Jun 14, 2004, in forum: Perl
    Replies:
    0
    Views:
    560
    Charles Shannon Hendrix
    Jun 14, 2004
  2. Rick Strahl [MVP]

    Capturing ASPX output from another page

    Rick Strahl [MVP], Jul 9, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    488
    Steve C. Orr, MCSD
    Jul 9, 2003
  3. PJ

    Capturing Request Stream

    PJ, Jul 17, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    570
  4. Jack Johnston
    Replies:
    5
    Views:
    572
    Natty Gur
    Jul 28, 2003
  5. Ted Sung
    Replies:
    1
    Views:
    330
    Sherm Pendley
    Aug 30, 2004
Loading...

Share This Page