Eddy Mulyono (eddymulyono) wrote,

HOWTO redirect stdout in Ruby

HOWTO redirect stdout in Ruby

In Ruby, stdout is $stdout.

To redirect stdout, say, to /dev/null, you can try:

orig_stdout = $stdout

# redirect stdout to /dev/null
$stdout = File.new('/dev/null', 'w')
puts('goes to /dev/null')

# restore stdout
$stdout = orig_stdout
puts('goes to stdout')

For future reference, my environment is:
  • Ubuntu Gutsy
  • Ruby 1.8.6
Tags: code, ruby, tech, work
  • Post a new comment


    default userpic

    Your reply will be screened