Module: IndentedIO::IndentedIOInterface

Included in:
IO, IndentedIO, StringIO, Tempfile
Defined in:
lib/indented_io/indented_io_interface.rb

Overview

IndentedIO interface provide the #indent method. It is used by IO, StringIO, and IndentedIO but can be included in any class that define a #write method like this:

require 'indented_io'

class MyIO
  include IndentedIO::IndentedIOInterface
  def write(*args) ... end
end

my_io = MyIO.new
my_io.print "Not indented\n"
my_io.indent.puts "It works!"

# Not indented
#   It works!

Instance Method Summary collapse

Instance Method Details

#depthObject

Return the accumulated number of indentation levels. An unindented device has depth equal to 0



51
# File 'lib/indented_io/indented_io_interface.rb', line 51

def depth() 0 end

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

Returns a IndentedIO object that can be used for printing. The IO object will pass-through all methods to the underlying device except #print, #printf, #puts, and #p that extends the default to output indented text

level is the number of levels to indent the following text. If level is negative, #indent will outdent text instead. +level’ can also be true or false: true sets the level to 1 and false sets it to 0

string is the string used for indentation. The indentation string can also be given as the keyword parameter :string. Default is the indent string of the outer level or IndentedIO.default_indent if this is the first level

:bol controls the beginning-of-line status: If true, #indent will begin writing with an indentation string as if it was at the beginning of the line. If false, it will only indent after the first newline. Default is true



39
40
41
42
43
# File 'lib/indented_io/indented_io_interface.rb', line 39

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 block parameters"
  @indented_io_object = ::IndentedIO::IndentedIO.send(:new, self, levels, string, bol)
  block_given? ? yield(@indented_io_object) : @indented_io_object
end

#tabObject

Return current tabulator position. This is the same as the combined length of the indentations. An unindented device has depth equal to 0



55
# File 'lib/indented_io/indented_io_interface.rb', line 55

def tab() 0 end