Discarding putc But Not puts

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 :eek:riginal_putc :putc
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
 
7

7stud --

Lars said:
What about

def putc(obj)
end

puts "Hello"
putc "W"

Did I miss something?

Yep, switching it back to normal.


Andrew said:
Could anyone please show me the light?

Try this:

putc 65
puts 65

def func
alias orig_putc putc

def putc(*args)
end

putc 66
puts 66

alias putc orig_putc
end

func

putc 67
puts 67


--output:--
A65
66
C67
 
A

Andrew Stewart

Try this:

def func
alias orig_putc putc

def putc(*args)
end

putc 66
puts 66

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. Maybe undefining putc first would stop the warning but it's
not immediately obvious to me how to do that.

Thanks for your help. I greatly appreciate it.

Regards,
Andy Stewart
 
M

Morton Goldberg

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 :eek:riginal_putc :putc
def putc(obj)
end
end

=> "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
 
7

7stud --

Andrew said:
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?

...
alias garbage putc
alias putc orig_putc
end
 
A

Andrew Stewart

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>

Thanks, Morton, that's a good, methodical way to determine what's
being called. Most helpful.

Regards,
Andy Stewart
 
A

Andrew Stewart

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?

...
alias garbage putc
alias putc orig_putc
end

Ah, I see. Adding another alias does indeed obviate the warning.
Thanks for the insight.

Regards,
Andy Stewart
 

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
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top