Module: Kernel

Defined in:
lib/indented_io/kernel.rb

Constant Summary collapse

@@INDENT_STACK =
[]

Instance Method Summary collapse

Instance Method Details

#indent(levels = 1, string_ = IndentedIO.default_indent, string: string_, bol: nil, &block) ⇒ Object

Like IndentedIO::IndentedIOInterface#indent except the underlying device is not the receiver (Kernel) but $stdout. Kernel#indent also allows a block without an argument. In that case it manipulates $stdout to print indented:

puts "Not indented
indent do
  puts "Indented"
  indent do
    puts "Even more indented"
  end
end

# Not indented
#   Indented
#     Even more indented

It called without a block, it indents $stdout. It should then be matched with a corresponding #undent



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/indented_io/kernel.rb', line 25

def indent(levels = 1, string_ = IndentedIO.default_indent, string: string_, bol: nil, &block)
  block.nil? || block.arity <= 1 or raise IndentedIO::Error.new "Wrong number of parameters"
  obj = IndentedIO::IndentedIO.send(:new, $stdout, levels, string, bol)
  if block_given?
    if block.arity == 1
      r = yield obj
    elsif block.arity == 0
      saved_stdout = $stdout
      begin
        $stdout = obj
        r = yield
      ensure
        $stdout = saved_stdout
      end
    end
    r
  else
    $stdout = obj
  end
end

#undentObject



46
47
48
# File 'lib/indented_io/kernel.rb', line 46

def undent
  $stdout = $stdout.send(:parent)
end