capturing standard out?

Discussion in 'Ruby' started by Giles Bowkett, Mar 19, 2007.

  1. Giles Bowkett, Mar 19, 2007
    #1
    1. Advertising

  2. Giles Bowkett

    Robert Dober Guest

    On 3/19/07, Giles Bowkett <> wrote:
    > Is there an easy way to capture standard out in Ruby, so that "puts"
    > throws its output into a buffer instead of just popping up on the
    > screen?
    >
    > --
    > Giles Bowkett
    > http://www.gilesgoatboy.org
    > http://gilesbowkett.blogspot.com
    > http://giles.tumblr.com/
    >
    >


    I have a scriopt producing output for bash, for running the test spec
    I plugin output into an array, more or less like this:

    class Output
    @data =[]
    class << self
    attr_reader :data
    end
    end
    if $TESTING then
    def Kernl.puts *args, &blk
    Output.data << args.join("")
    Output.data << blk.call if blk
    end
    end

    I have also Output.data.clear in #setup of the testsuite.

    HTH
    Robert
    --
    You see things; and you say Why?
    But I dream things that never were; and I say Why not?
    -- George Bernard Shaw
     
    Robert Dober, Mar 19, 2007
    #2
    1. Advertising

  3. Giles Bowkett

    Gary Wright Guest

    On Mar 19, 2007, at 6:18 PM, Giles Bowkett wrote:

    > Is there an easy way to capture standard out in Ruby, so that "puts"
    > throws its output into a buffer instead of just popping up on the
    > screen?


    This is exactly the problem shell IO redirection was designed to solve.

    $ ruby script.rb > output

    What is the use case that prevents you from using shell redirection?



    Gary Wright
     
    Gary Wright, Mar 19, 2007
    #3
  4. On Tue, 2007-03-20 at 07:18 +0900, Giles Bowkett wrote:
    > Is there an easy way to capture standard out in Ruby, so that "puts"
    > throws its output into a buffer instead of just popping up on the
    > screen?


    You can set the $stdout global variable to point to whatever object you
    like, as long as it adheres to a simple interface. Kernel#puts simply
    redirects to $stdout.


    Cheers,
    Daniel
     
    Daniel Schierbeck, Mar 19, 2007
    #4
  5. Giles Bowkett

    Robert Dober Guest

    On 3/19/07, Daniel Schierbeck <> wrote:
    > On Tue, 2007-03-20 at 07:18 +0900, Giles Bowkett wrote:
    > > Is there an easy way to capture standard out in Ruby, so that "puts"
    > > throws its output into a buffer instead of just popping up on the
    > > screen?

    >
    > You can set the $stdout global variable to point to whatever object you
    > like, as long as it adheres to a simple interface. Kernel#puts simply
    > redirects to $stdout.

    That is a good idea too, especially with this idiom

    def xxx(..., out = $stdout)

    Robert
    >
    >
    > Cheers,
    > Daniel
    >
    >
    >



    --
    You see things; and you say Why?
    But I dream things that never were; and I say Why not?
    -- George Bernard Shaw
     
    Robert Dober, Mar 19, 2007
    #5
  6. On 3/19/07, Daniel Schierbeck <> wrote:
    > On Tue, 2007-03-20 at 07:18 +0900, Giles Bowkett wrote:
    > > Is there an easy way to capture standard out in Ruby, so that "puts"
    > > throws its output into a buffer instead of just popping up on the
    > > screen?

    >
    > You can set the $stdout global variable to point to whatever object you
    > like, as long as it adheres to a simple interface. Kernel#puts simply
    > redirects to $stdout.
    >
    >
    > Cheers,
    > Daniel


    Hey Daniel, I found an example of the $stdout technique from Matz:

    http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/15519

    That's what I ended up using, although I used $stdout and instead of
    $defout, and the documentation I found said assigning to $stdout is
    deprecated in favor of $stdout.reopen, which I couldn't seem to get to
    work for me.


    --
    Giles Bowkett
    http://www.gilesgoatboy.org
    http://gilesbowkett.blogspot.com
    http://giles.tumblr.com/
     
    Giles Bowkett, Mar 20, 2007
    #6
  7. Giles Bowkett, Mar 20, 2007
    #7
  8. Here's a code snippet from my log library.

    gegroet,
    Erik V. - http://www.erikveen.dds.nl/

    ----------------------------------------------------------------

    ["$stdout", "$stderr"].each do |std|
    io = eval(std)
    old_write = io.method:)write)

    class << io
    self
    end.module_eval do
    define_method:)write) do |text|
    unless text =~ /^[\r\n]+$/ # Because puts calls twice.
    File.open("logfile.log", "a") do |f|
    f.puts [std[1..-1].upcase, caller[2], text].join(" ")
    end
    end

    old_write.call(text)
    end
    end
    end

    $stdout.puts "text on stdout"
    $stderr.puts "text on stderr"

    ----------------------------------------------------------------
     
    Erik Veenstra, Mar 20, 2007
    #8
  9. On Mar 19, 2007, at 5:18 PM, Giles Bowkett wrote:

    > Is there an easy way to capture standard out in Ruby, so that "puts"
    > throws its output into a buffer instead of just popping up on the
    > screen?


    I would do it like this:

    #!/usr/bin/env ruby -w

    require "stringio"

    def capture_stdout(buffer = StringIO.new)
    saved_stdout = $stdout
    $stdout = buffer

    yield

    $stdout = saved_stdout

    buffer.string rescue buffer
    end

    if __FILE__ == $PROGRAM_NAME
    puts "This will be printed."
    output = capture_stdout { puts "Meanwhile, this was captured." }
    puts "This also will be printed."
    p output
    end

    __END__

    James Edward Gray II
     
    James Edward Gray II, Mar 26, 2007
    #9
    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. steve.leach

    How standard is the standard library?

    steve.leach, Apr 18, 2005, in forum: Python
    Replies:
    1
    Views:
    393
    Christos TZOTZIOY Georgiou
    Apr 18, 2005
  2. funkyj
    Replies:
    5
    Views:
    1,132
    funkyj
    Jan 20, 2006
  3. Replies:
    5
    Views:
    868
  4. Sriram Srinivasan
    Replies:
    13
    Views:
    567
    Benjamin Kaplan
    Nov 12, 2009
  5. Venks
    Replies:
    5
    Views:
    242
    Ken Bloom
    Dec 6, 2007
Loading...

Share This Page