Top Level Namespace

Includes:
ExamplePrinter

Defined Under Namespace

Modules: Console, Enumerable, ExamplePrinter, FileTest, Find, Kernel, Test, WithKnowledgeOfColor Classes: Array, Date, Dir, Exception, File, Hash, Module, Month, Mutex, NilClass, Object, Regexp, String, Symbol, Time

Instance Method Summary collapse

Methods included from ExamplePrinter

#put_statement, #xmp

Instance Method Details

#filter_stderr(filter, &block) ⇒ Object

Applies filter to any $stderr output that block tries to generate.

filter should be a Proc that accepts attempted_output as its parameter and returns the string that should actually be output.

filter_stderr(lambda{''}) do
  noisy_command
end


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/qualitysmith_extensions/kernel/filter_output.rb', line 18

def filter_stderr(filter, &block)
  old_stderr = $stderr
  $stderr = StringIO.new
  begin
    yield
  ensure
    what_they_tried_to_output = $stderr.string
    $stderr = old_stderr
    $stderr.print filter.call(what_they_tried_to_output)
  end
end

#filter_stdout(filter, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/qualitysmith_extensions/kernel/filter_output.rb', line 29

def filter_stdout(filter, &block)
  old_stderr = $stdout
  $stdout = StringIO.new
  begin
    yield
  ensure
    what_they_tried_to_output = $stdout.string
    $stdout = old_stderr
    $stdout.print filter.call(what_they_tried_to_output)
  end
end

#simulate_input(input_string, &block) ⇒ Object

Simulates a user typing in input_string on the keyboard.

Useful for testing console apps that are ordinarily (they prompt the user for input).

output = simulate_inpute('foo') do
  input = $stdin.gets
  capture_output { do_stuff() }
end


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/qualitysmith_extensions/kernel/simulate_input.rb', line 21

def simulate_input(input_string, &block)

  original_stdin = $stdin
  $stdin = StringIO.new(input_string, 'r')

  begin
    yield
  rescue Exception
    raise
  ensure
    $stdin = original_stdin
  end
end