Class: Switch

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

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.in(input) ⇒ Object



5
6
7
8
9
10
11
12
# 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) ⇒ Object



14
15
16
17
18
19
20
21
22
# 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) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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

Instance Method Details

#in(input) ⇒ Object



50
51
52
# File 'lib/switch.rb', line 50

def in(input)
  self.class.in(input)
end

#out(return_io = false) ⇒ Object



53
54
55
# File 'lib/switch.rb', line 53

def out(return_io=false)
  self.class.out(return_io)
end

#up(options) ⇒ Object



56
57
58
# File 'lib/switch.rb', line 56

def up(options)
  self.class.up(options)
end