Module: Pione::Util::Indentation

Defined in:
lib/pione/util/indentation.rb

Class Method Summary collapse

Class Method Details

.cut(text) ⇒ String

Cut indentations of the text. This function cuts indentation of all lines with the depth of first line's indentation.

Parameters:

  • text (String)

    the text

Returns:

  • (String)

    indentation cutted text



12
13
14
15
16
# File 'lib/pione/util/indentation.rb', line 12

def self.cut(text)
  line = text.lines.to_a.first
  n = line.length - line.lstrip.length
  n > 0 ? text.gsub(/^[ ]{0,#{n}}/m, "") : text
end

.indent(text, size) ⇒ String

Add Indentation to the text.

Parameters:

  • text (String)

    the text

  • size (Integer)

    indentaion size

Returns:

  • (String)

    indented text



26
27
28
29
30
# File 'lib/pione/util/indentation.rb', line 26

def self.indent(text, size)
  text.lines.each_with_object("") do |line, indented|
    indented << " " * size + line
  end
end