Parent

Class/Module Index [+]

Quicksearch

String

Public Instance Methods

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'
# 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
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 = <<-END.strip_indent
  def test
    some_method
    other_method
  end
END

#=> "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 = <<-END.strip_margin('|')
  |def test
  |  some_method
  |  other_method
  |end
END

#=> "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

[Validate]

Generated with the Darkfish Rdoc Generator 2.