module Nuggets::IO::RedirectMixin

Public Instance Methods

redirect(target = ::File::NULL, mode = 'w') { || ... } click to toggle source
# File lib/nuggets/io/redirect_mixin.rb, line 31
def redirect(target = ::File::NULL, mode = 'w')
  unless target.is_a?(self.class)
    target = ::File.open(target, mode)
    close_target = true
  end

  prev = dup
  reopen(target)

  yield
ensure
  reopen(prev) if prev
  target.close if close_target
end