Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/coolkit/string/indent.rb,
lib/coolkit/string/comment.rb

Overview

Straight from ActiveSupport

Instance Method Summary collapse

Instance Method Details

#comment(indent_empty_lines = true) ⇒ String

Returns a copy of a string with a comment ("# ") after every newline in a string.

Parameters:

  • indent_empty_lines (Boolean) (defaults to: true)

Returns:



16
17
18
# File 'lib/coolkit/string/comment.rb', line 16

def comment(indent_empty_lines = true)
  dup.tap { |s| s.comment!(indent_empty_lines) }
end

#comment!(indent_empty_lines = true) ⇒ void

This method returns an undefined value.

Adds a comment string ("# ") after every newline in a string.

Parameters:

  • indent_empty_lines (Boolean) (defaults to: true)


7
8
9
10
# File 'lib/coolkit/string/comment.rb', line 7

def comment!(indent_empty_lines = true)
  re = indent_empty_lines ? /^/ : /^(?!$)/
  gsub!(re, "# ")
end

#indent(amount, indent_string = nil, indent_empty_lines = false) ⇒ String

Indents the lines in the receiver.

The second argument, indent_string, specifies which indent string to use. The default is nil, which tells the method to make a guess by peeking at the first indented line, and fallback to a space if there is none.

While indent_string is typically one space or tab, it may be any string.

The third argument, indent_empty_lines, is a flag that says whether empty lines should be indented. Default is false.

Parameters:

  • amount (Integer)

    The level of indentation to add.

  • indent_string (String) (defaults to: nil)

    Defaults to tab if a tab is present in the string, and " " otherwise.

  • indent_empty_lines (Boolean) (defaults to: false)

Returns:



38
39
40
# File 'lib/coolkit/string/indent.rb', line 38

def indent(amount, indent_string = nil, indent_empty_lines = false)
  dup.tap { |s| s.indent!(amount, indent_string, indent_empty_lines) }
end

#indent!(amount, indent_string = nil, indent_empty_lines = false) ⇒ void

This method returns an undefined value.

Same as #indent, except it indents the receiver in-place.

Returns the indented string, or nil if there was nothing to indent.

Parameters:

  • amount (Integer)

    The level of indentation to add.

  • indent_string (String) (defaults to: nil)

    Defaults to tab if a tab is present in the string, and " " otherwise.

  • indent_empty_lines (Boolean) (defaults to: false)


15
16
17
18
19
# File 'lib/coolkit/string/indent.rb', line 15

def indent!(amount, indent_string = nil, indent_empty_lines = false)
  indent_string = indent_string || self[/^[ \t]/] || " "
  re = indent_empty_lines ? /^/ : /^(?!$)/
  gsub!(re, indent_string * amount)
end