Class: CatchAndRelease::Catch::Stdout

Inherits:
Object
  • Object
show all
Defined in:
lib/catch_and_release/catch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStdout

Returns a new instance of Stdout.



22
23
24
25
# File 'lib/catch_and_release/catch.rb', line 22

def initialize
  @orig_stdout = $stdout
  @new_stdout  = StringIO.new
end

Instance Attribute Details

#new_stdoutObject (readonly)

Returns the value of attribute new_stdout.



20
21
22
# File 'lib/catch_and_release/catch.rb', line 20

def new_stdout
  @new_stdout
end

#orig_stdoutObject (readonly)

Returns the value of attribute orig_stdout.



19
20
21
# File 'lib/catch_and_release/catch.rb', line 19

def orig_stdout
  @orig_stdout
end

Instance Method Details

#capture(&block) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/catch_and_release/catch.rb', line 27

def capture &block
  $stdout = new_stdout
  yield
  return self
ensure
  $stdout = orig_stdout
end

#readObject



35
36
37
38
# File 'lib/catch_and_release/catch.rb', line 35

def read
  new_stdout.rewind
  new_stdout.read
end