module Kernel

Public Instance Methods

indent(levels = 1, string_ = IndentedIO.default_indent, string: string_, bol: nil) { |obj| ... } click to toggle source

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
# File lib/indented_io/kernel.rb, line 20
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
  else
    r = obj
  end
  r
end