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
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.
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
···
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
Since I think other people satisfactorily answered the rest, note here
that "b" is a return value, and that a normal putc would look like this
on a console:
Thanks, Morton, that's a good, methodical way to determine what's being called. Most helpful.
Regards,
Andy Stewart
···
On 2 Oct 2007, at 14:29, Morton Goldberg wrote:
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
It produces a warning ("discarding old putc") when it encounters the second alias, at the end of the target method. But I can live with that. Maybe undefining putc first would stop the warning but it's not immediately obvious to me how to do that.
It produces a warning ("discarding old putc") when it encounters the
second alias, at the end of the target method. But I can live with
that.
I think the warning is saying, "Hey, there won't be any reference to the
putc def inside func if you do that last alias. Well, how about using
another alias to save the putc def in?
Ah, I see. Adding another alias does indeed obviate the warning. Thanks for the insight.
Regards,
Andy Stewart
···
On 2 Oct 2007, at 16:54, 7stud -- wrote:
Andrew Stewart wrote:
On 2 Oct 2007, at 13:36, 7stud -- wrote:
alias putc orig_putc
end
Aliasing back again worked -- thanks.
It produces a warning ("discarding old putc") when it encounters the
second alias, at the end of the target method. But I can live with
that.
I think the warning is saying, "Hey, there won't be any reference to the
putc def inside func if you do that last alias. Well, how about using
another alias to save the putc def in?