class String

Public Instance Methods

ascii_only() click to toggle source
# File lib/powerpack/string/ascii_only.rb, line 14
def ascii_only
  dup.ascii_only!
end
ascii_only!() click to toggle source
# File lib/powerpack/string/ascii_only.rb, line 31
def ascii_only!
  encoding_options = {
    :invalid                     => :replace,  # Replace invalid byte sequences
    :undef                       => :replace,  # Replace anything not defined in ASCII
    :replace                     => '',        # Use a blank for those replacements
    :UNIVERSAL_NEWLINE_DECORATOR => true       # Always break lines with \n
  }
  self.encode! Encoding.find('ASCII'), encoding_options
end
blank?() click to toggle source

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
# File lib/powerpack/string/blank.rb, line 16
def blank?
  empty? || strip.empty?
end
format(*args) click to toggle source

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'
Calls superclass method
# File lib/powerpack/string/format.rb, line 16
def format(*args)
  super(self, *(args.flatten(1)))
end
remove(pattern) click to toggle source

Removes all occurrences of a pattern in a string.

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

# File lib/powerpack/string/remove.rb, line 6
def remove(pattern)
  dup.remove!(pattern)
end
remove!(pattern) click to toggle source

Removes all occurrences of a pattern in a string.

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

# File lib/powerpack/string/remove.rb, line 13
def remove!(pattern)
  gsub!(pattern, '')
end
remove_prefix(pattern) click to toggle source

Removes a prefix in a string.

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

@example

'Ladies Night'.remove_prefix('Ladies ') #=> 'Night'
# File lib/powerpack/string/remove_prefix.rb, line 9
def remove_prefix(pattern)
  dup.remove_prefix!(pattern)
end
remove_prefix!(pattern) click to toggle source

Removes a prefix in a string.

@return [String] the string without the prefix.

@example

'Ladies Night'.remove_prefix!('Ladies ') #=> 'Night'
# File lib/powerpack/string/remove_prefix.rb, line 19
def remove_prefix!(pattern)
  gsub!(/\A#{pattern}/, '')
end
remove_suffix(pattern) click to toggle source

Removes a suffix in a string.

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

@example

'Ladies Night'.remove_suffix(' Night') #=> 'Ladies'
# File lib/powerpack/string/remove_suffix.rb, line 9
def remove_suffix(pattern)
  dup.remove_suffix!(pattern)
end
remove_suffix!(pattern) click to toggle source

Removes a suffix in a string.

@return [String] the string without the suffix.

@example

'Ladies Night'.remove_suffix!(' Night') #=> 'Ladies'
# File lib/powerpack/string/remove_suffix.rb, line 19
def remove_suffix!(pattern)
  gsub!(/#{pattern}\z/, '')
end
squish() click to toggle source

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'
# File lib/powerpack/string/squish.rb, line 10
def squish
  dup.squish!
end
squish!() click to toggle source

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'
# File lib/powerpack/string/squish.rb, line 21
def squish!
  strip!
  gsub!(/\s+/, ' ')
  self
end
strip_indent() click to toggle source

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

@example

code = "  def test
    some_method
    other_method
  end
".strip_indent

#=> "def\n  some_method\n  \nother_method\nend"
# File lib/powerpack/string/strip_indent.rb, line 16
def strip_indent
  leading_space = scan(/^[ \t]*(?=\S)/).min
  indent = leading_space ? leading_space.size : 0
  gsub(/^[ \t]{#{indent}}/, '')
end
strip_margin(margin_characters) click to toggle source

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

@example

code = "  |def test
  |  some_method
  |  other_method
  |end
".strip_margin('|')

#=> "def\n  some_method\n  \nother_method\nend"
# File lib/powerpack/string/strip_margin.rb, line 16
def strip_margin(margin_characters)
  margin = Regexp.quote(margin_characters)
  gsub(/^\s+#{margin}/, '')
end