Stream filtering

I’m looking for a way to filter the stdout/stderr streams in ruby.
In python, you can point sys.stdout to any class with a ‘write’ method-
the write method could then be used to perform filtering actions on
printed text. If it is possible, I would really appreciate a sample. If it
isnt, i’d appreciate suggestions. I think I might try replacing the puts
function, or something like that.

Thanks
–Segphault

printed text. If it is possible, I would really appreciate a sample. If it

Something like this ?

svg% cat b.rb
#!/usr/bin/ruby
class A
   def initialize
      @io = File.new("aa", "w")
   end

   def write(mess)
      @io.write(mess)
   end
end

$stdout = A.new
puts "hello"
svg%

svg% b.rb
svg%

svg% cat aa
hello
svg%

Guy Decoux

a = nil
def a.write(str)
STDOUT << str.upcase
end

$stdout = a

puts “hallo” # => “HALLO”

Regards,

Michael

···

On Wed, May 19, 2004 at 12:43:49AM +0900, Ryan Paul wrote:

I’m looking for a way to filter the stdout/stderr streams in ruby.
In python, you can point sys.stdout to any class with a ‘write’ method-
the write method could then be used to perform filtering actions on
printed text. If it is possible, I would really appreciate a sample. If it
isnt, i’d appreciate suggestions. I think I might try replacing the puts
function, or something like that.

“ts” decoux@moulon.inra.fr schrieb im Newsbeitrag
news:200405181548.i4IFmAX17388@moulon.inra.fr

printed text. If it is possible, I would really appreciate a sample. If
it

Something like this ?

svg% cat b.rb
#!/usr/bin/ruby
class A
def initialize
@io = File.new(“aa”, “w”)
end

def write(mess)
@io.write(mess)
end
end

$stdout = A.new
puts “hello”
svg%

svg% b.rb
svg%

svg% cat aa
hello
svg%

I thoughz #reopen was considered better because it keeps the reference to
the same instance:

$stdout.reopen( File.open(“foo”, “w”) )
puts “hallo”

Regards

robert