Class String
In: lib/powerpack/string/strip_margin.rb
lib/powerpack/string/remove.rb
lib/powerpack/string/squish.rb
lib/powerpack/string/remove_suffix.rb
lib/powerpack/string/strip_indent.rb
lib/powerpack/string/format.rb
lib/powerpack/string/blank.rb
lib/powerpack/string/remove_prefix.rb
Parent: Object

Methods

Public Instance methods

Checks whether a string is blank. A string is considered blank if it is either empty or contains only whitespace characters.

@return [Boolean] true is the string is blank, false otherwise

@example

  ''.blank? #=> true

@example

  '    '.blank? #=> true

@example

  '  test'.blank? #=> false

A nicer alternative to Kernel#sprintf and String#%.

@return [String] the formatted string

@example

  'This is %s!'.format('Sparta') #=> 'This is Sparta!'

@example

  'My name is %{fname} %{lname}.'.format(fname: 'Bruce', lname: 'Wayne')
  #=> 'My name is Bruce Wayne.'

@example

  '%d + %d'.format([1, 2]) #=> '1 + 2'

Removes all occurrences of a pattern in a string.

@return [String] a new string without any occurrences of the pattern.

Removes all occurrences of a pattern in a string.

@return [String] the string without any occurrences of the pattern.

Removes a prefix in a string.

@return [String] a new string without the prefix.

@example

  'Ladies Night'.remove_prefix('Ladies ') #=> 'Night'

Removes a prefix in a string.

@return [String] the string without the prefix.

@example

  'Ladies Night'.remove_prefix!('Ladies ') #=> 'Night'

Removes a suffix in a string.

@return [String] a new string without the suffix.

@example

  'Ladies Night'.remove_suffix(' Night') #=> 'Ladies'

Removes a suffix in a string.

@return [String] the string without the suffix.

@example

  'Ladies Night'.remove_suffix!(' Night') #=> 'Ladies'

Strips leading and trailing whitespace and squashes internal whitespace.

@return [String] a new string with no leading and trailing

  whitespace and no consecutive whitespace characters inside it

@example

  ' Peter   Parker'.squish #=> 'Peter Parker'

Strips leading and trailing whitespace and squashes internal whitespace.

@return [String] the string with no leading and trailing whitespace and no

  consecutive whitespace characters inside it

@example

  ' Peter   Parker'.squish #=> 'Peter Parker'

The method strips the whitespace preceding the base indentation. Useful for HEREDOCs and other multi-line strings.

@example

  code = <<-END.strip_indent
    def test
      some_method
      other_method
    end
  END

  #=> "def\n  some_method\n  \nother_method\nend"

The method strips the characters preceding a special margin character. Useful for HEREDOCs and other multi-line strings.

@example

  code = <<-END.strip_margin('|')
    |def test
    |  some_method
    |  other_method
    |end
  END

  #=> "def\n  some_method\n  \nother_method\nend"

[Validate]