I am using a compiled C-code in my Ruby program. The problem is that
the C-code prints some messages that I don’t want to show in my Ruby
program. What would be the best way to get rid of these printouts?
I don’t want to get into the original C-code and take away the print
commands since that is not my code. Are the printouts sent directly to
standard output, meaning there is no way for Ruby to interfere?
I am using a compiled C-code in my Ruby program. The problem is that
the C-code prints some messages that I don’t want to show in my Ruby
program. What would be the best way to get rid of these printouts?
I don’t want to get into the original C-code and take away the print
commands since that is not my code. Are the printouts sent directly to
standard output, meaning there is no way for Ruby to interfere?
You may try to redirect $stdout while the C code runs - I’m not sure
this will work, but it’s worth a try.
I am using a compiled C-code in my Ruby program. The problem is that
the C-code prints some messages that I don’t want to show in my Ruby
program. What would be the best way to get rid of these printouts?
I don’t want to get into the original C-code and take away the print
commands since that is not my code. Are the printouts sent directly to
standard output, meaning there is no way for Ruby to interfere?
What’s the difference between $stdout and STDOUT? They seem to be
the same thing.
In my pickaxe book I read that “Assignment to $stdout is
depracated; use $stdout.reopen instead.” But if I want to redirect
$stdout and then $direct it back to what it was before, surely I HAVE
to use assignment to $stdout. Like this:
out = $stdout
$stdout = File.new(’/dev/null’,‘w’)
puts ‘This is not to be seen by anybody.’
$stdout.reopen(out)
If I use reopen on the second line instead, the ‘out’ variable comes
along to this new value and nobody knows where the “usual stdout” is,
right?
I am still confused over why STDOUT is not a constant though…
Thanks for your pointers!
I’d guess that when you reopen a stream, then it as an object remains
the same, just what you send it will end up somewhere else than before.
In contrast, when you assign to a variable, the object itself changes
which that variable references.
Since $stdout and STDOUT initially point to the same object, if you dont
assign $stdout, instead you modify the onject itself, the two continue
to reference the same original object. Remember that you can modify a
constant object’s state without any warning anyway, so you could even
reopen STDOUT itself.
In your case you want to reopen STDOUT because somebody using your
code may have changed $stdout, if they want to capture or redirect
output.
In my pickaxe book I read that “Assignment to $stdout is
depracated; use $stdout.reopen instead.” But if I want to redirect
$stdout and then $direct it back to what it was before, surely I HAVE
to use assignment to $stdout. Like this:
I don’t think this is true anymore. You can’t reopen using StringIO
to capture regular ruby puts or p.
out = $stdout
$stdout = File.new(‘/dev/null’,‘w’)
puts ‘This is not to be seen by anybody.’
$stdout.reopen(out)
If I use reopen on the second line instead, the ‘out’ variable comes
along to this new value and nobody knows where the “usual stdout” is,
right?
Right, you should dup $stdout there.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.