Module: CommandKit::Stdio
- Defined in:
- lib/command_kit/stdio.rb
Overview
Provides access to stdin, stdout, and stderr streams.
Examples
class MyCmd
include CommandKit::Stdio
def run
print 'Name: '
name = gets
puts "Hello #{name}!"
end
end
Testing
Can be initialized with custom stdin, stdout, and stderr streams for testing purposes.
stdin = StringIO.new
stdout = StringIO.new
stderr = StringIO.new
MyCmd.new(stdin: stdin, stdout: stdout, stderr: stderr)
Instance Method Summary collapse
-
#abort(message = nil) ⇒ Object
Overrides
Kernel.abort
to print to #stderr. -
#gets(*arguments) ⇒ Object
Calls
stdin.gets
. - #initialize(stdin: nil, stdout: nil, stderr: nil, **kwargs) ⇒ Object
-
#print(*arguments) ⇒ Object
Calls
stdout.print
. -
#printf(*arguments) ⇒ Object
Calls
stdout.printf
. -
#putc(*arguments) ⇒ Object
Calls
stdout.putc
. -
#puts(*arguments) ⇒ Object
Calls
stdout.puts
. -
#readline(*arguments) ⇒ Object
Calls
stdin.readline
. -
#readlines(*arguments) ⇒ Object
Calls
stdin.readlines
. -
#stderr ⇒ $stderr, IO
Returns the stderr error output stream.
-
#stdin ⇒ $stdin, IO
Returns the stdin input stream.
-
#stdout ⇒ $stdout, IO
Returns the stdout output stream.
Instance Method Details
#abort(message = nil) ⇒ Object
Overrides Kernel.abort
to print to #stderr.
163 164 165 166 |
# File 'lib/command_kit/stdio.rb', line 163 def abort(=nil) stderr.puts() if exit(1) end |
#gets(*arguments) ⇒ Object
Calls stdin.gets
.
94 95 96 |
# File 'lib/command_kit/stdio.rb', line 94 def gets(*arguments) stdin.gets(*arguments) end |
#initialize(stdin: nil, stdout: nil, stderr: nil, **kwargs) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/command_kit/stdio.rb', line 45 def initialize(stdin: nil, stdout: nil, stderr: nil, **kwargs) @stdin = stdin @stdout = stdout @stderr = stderr super(**kwargs) end |
#print(*arguments) ⇒ Object
Calls stdout.print
.
142 143 144 |
# File 'lib/command_kit/stdio.rb', line 142 def print(*arguments) stdout.print(*arguments) end |
#printf(*arguments) ⇒ Object
Calls stdout.printf
.
151 152 153 |
# File 'lib/command_kit/stdio.rb', line 151 def printf(*arguments) stdout.printf(*arguments) end |
#putc(*arguments) ⇒ Object
Calls stdout.putc
.
124 125 126 |
# File 'lib/command_kit/stdio.rb', line 124 def putc(*arguments) stdout.putc(*arguments) end |
#puts(*arguments) ⇒ Object
Calls stdout.puts
.
133 134 135 |
# File 'lib/command_kit/stdio.rb', line 133 def puts(*arguments) stdout.puts(*arguments) end |
#readline(*arguments) ⇒ Object
Calls stdin.readline
.
103 104 105 |
# File 'lib/command_kit/stdio.rb', line 103 def readline(*arguments) stdin.readline(*arguments) end |
#readlines(*arguments) ⇒ Object
Calls stdin.readlines
.
112 113 114 |
# File 'lib/command_kit/stdio.rb', line 112 def readlines(*arguments) stdin.readlines(*arguments) end |
#stderr ⇒ $stderr, IO
Returns the stderr error output stream.
85 86 87 |
# File 'lib/command_kit/stdio.rb', line 85 def stderr @stderr || $stderr end |
#stdin ⇒ $stdin, IO
Returns the stdin input stream.
61 62 63 |
# File 'lib/command_kit/stdio.rb', line 61 def stdin @stdin || $stdin end |
#stdout ⇒ $stdout, IO
Returns the stdout output stream.
73 74 75 |
# File 'lib/command_kit/stdio.rb', line 73 def stdout @stdout || $stdout end |