Morton Goldberg
10/2/2007 1:30:00 PM
On Oct 2, 2007, at 7:02 AM, Andrew Stewart wrote:
> 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 :original_putc :putc
> def putc(obj)
> end
> end
>
> >> putc 'b'
> => "b" # I was hoping for no output
>
> Could anyone please show me the light?
Running the following code may cast some light on your problem.
<code>
class << $stdout
def putc(obj)
puts "1 called"
end
end
class << STDOUT
def putc(obj)
puts "2 called"
end
end
module Kernel
def putc(obj)
puts "3 called"
end
end
putc 'b'
</code>
Regards, Morton