Created
September 24, 2010 20:44
-
-
Save macek/596007 to your computer and use it in GitHub Desktop.
Output Buffering with Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A contents <<< | |
send this to a | |
this goes to a | |
a, I forgot to add this | |
<<< | |
B contents <<< | |
this goes to b | |
b, please | |
<<< |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = OutputBuffer.new | |
b = OutputBuffer.new | |
a.activate | |
puts "send this to a" | |
puts "this goes to a" | |
b.activate | |
puts "this goes to b" | |
puts "b, please" | |
a.activate | |
puts "a, I forgot to add this" | |
OutputBuffer::restore_default | |
puts "A contents <<<\n#{a}<<<" | |
puts "B contents <<<\n#{b}<<<" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :example do | |
desc "Some important task" | |
task :notify, :send_email, :needs => [:environment] do |task, args| | |
args.with_defaults(:send_email => false) | |
buffer = OutputBuffer.new | |
puts "this would ordinarily be displayed right away" | |
buffer.stop | |
if args[:send_email] | |
# could send email here | |
# UserMailer.deliver_notification_mail(buffer) | |
puts "sending email <<<\n#{buffer}<<<" | |
else | |
puts buffer | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "stringio" | |
class OutputBuffer | |
def initialize | |
@buffer = StringIO.new | |
activate | |
end | |
def activate | |
$stdout = @buffer | |
end | |
def to_s | |
@buffer.rewind | |
@buffer.read | |
end | |
def stop | |
OutputBuffer::restore_default | |
end | |
def self.restore_default | |
$stdout = STDOUT | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "stringio" | |
buffer = StringIO.new | |
$stdout = buffer | |
puts "this would ordinarily be displayed right away" | |
$stdout = STDOUT | |
buffer.rewind | |
puts "the buffer captured: #{buffer.read}" | |
# => the buffer captured: this would ordinarily be displayed right away |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rake example:notify | |
this would ordinarily be displayed right away | |
$ rake example:notify[true] | |
sending email <<< | |
this would ordinarily be displayed right away | |
<<< |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment