stream filtering

R

Ryan Paul

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
 
T

ts

R> 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
 
M

Michael Neumann

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.

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

$stdout = a

puts "hallo" # => "HALLO"

Regards,

Michael
 
R

Robert Klemme

ts said:
R> 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
 

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
474,145
Messages
2,570,826
Members
47,373
Latest member
Desiree036

Latest Threads

Top