A
Andrew Stewart
Hello,
I'm working with a third-party library that calls both putc and puts
in a certain method. I'd like to discard what is written with putc
for the duration of the method while keeping anything written to
puts. Given that the method lets you hook in your own code at its
start and end, how do I do this?
I've tried various approaches including replacing $stdout with a
custom class, and opening $stdout and rewriting putc -- but I haven't
quite succeeded.
For example, here's some code that doesn't work:
class << $stdout
alias
riginal_putc
utc
def putc(obj)
end
end
=> "b" # I was hoping for no output
And some more non-working code:
class MyIO < StringIO
def putc(obj)
end
end
# In hook called at start of target method
$original_stdout = $stdout
$stdout = MyIO.new
# In hook called at end of target method
$stdout = $original_stdout
Result: both putc and puts appear to be discarded.
Could anyone please show me the light?
Thanks in advance,
Andy Stewart
I'm working with a third-party library that calls both putc and puts
in a certain method. I'd like to discard what is written with putc
for the duration of the method while keeping anything written to
puts. Given that the method lets you hook in your own code at its
start and end, how do I do this?
I've tried various approaches including replacing $stdout with a
custom class, and opening $stdout and rewriting putc -- but I haven't
quite succeeded.
For example, here's some code that doesn't work:
class << $stdout
alias
def putc(obj)
end
end
=> "b" # I was hoping for no output
And some more non-working code:
class MyIO < StringIO
def putc(obj)
end
end
# In hook called at start of target method
$original_stdout = $stdout
$stdout = MyIO.new
# In hook called at end of target method
$stdout = $original_stdout
Result: both putc and puts appear to be discarded.
Could anyone please show me the light?
Thanks in advance,
Andy Stewart