class Switch

Constants

VERSION

Public Class Methods

in(input) { || ... } click to toggle source
# File lib/switch.rb, line 5
def in(input)
  stdin  = $stdin
  $stdin = StringIO.new(input).tap {|io| io.rewind}
  yield
  $stdin.read
ensure
  $stdin = stdin
end
out(return_io=false) { || ... } click to toggle source
# File lib/switch.rb, line 14
def out(return_io=false)
  pipe    = StringIO.new
  stdout  = $stdout
  $stdout = pipe
  yield
  return_io ? pipe : pipe.string
ensure
  $stdout = stdout
end
up(options) { || ... } click to toggle source
# File lib/switch.rb, line 24
def up(options)
  defaults = {
    stdout: false,
    stdin:  nil
  }
  options = defaults.merge options
  
  stdin  = $stdin
  stdout = $stdout

  $stdin  = StringIO.new(options[:stdin]).tap {|io| io.rewind}
  pipe    = StringIO.new
  $stdout = pipe

  yield
  
  return_values = {}
  return_values[:out] = options[:stdout] ? pipe : pipe.string
  return_values[:in]  = $stdin.read
  return_values
ensure
  $stdin  = stdin
  $stdout = stdout
end

Public Instance Methods

in(input) click to toggle source
# File lib/switch.rb, line 50
def in(input)
  self.class.in(input)
end
out(return_io=false) click to toggle source
# File lib/switch.rb, line 53
def out(return_io=false)
  self.class.out(return_io)
end
up(options) click to toggle source
# File lib/switch.rb, line 56
def up(options)
  self.class.up(options)
end