Skip to content

Instantly share code, notes, and snippets.

@jwo
Created January 1, 2013 21:09
Show Gist options
  • Save jwo/4430028 to your computer and use it in GitHub Desktop.
Save jwo/4430028 to your computer and use it in GitHub Desktop.
JRuby capture of System.out.println
def silencer
require 'java'
java_import 'java.io.PrintStream'
java_import 'java.io.ByteArrayOutputStream'
java_import 'java.lang.System'
sys_out_stream = System.out
my_output_stream = ByteArrayOutputStream.new
System.setOut(PrintStream.new(my_output_stream))
yield
my_output_stream.toString
ensure
System.setOut(sys_out_stream)
end
output= silencer do
System.out.println "Tacos Rule"
puts "yo"
end
puts "output: #{output}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment