You are viewing eddymulyono

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: , , ,

Comments

July 2011

S M T W T F S
     12
3456789
10111213141516
17181920212223
24252627282930
31      
Powered by LiveJournal.com