Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/powerpack/string/blank.rb,
lib/powerpack/string/format.rb,
lib/powerpack/string/remove.rb,
lib/powerpack/string/squish.rb,
lib/powerpack/string/ascii_only.rb,
lib/powerpack/string/strip_indent.rb,
lib/powerpack/string/strip_margin.rb,
lib/powerpack/string/remove_prefix.rb,
lib/powerpack/string/remove_suffix.rb

Instance Method Summary collapse

Instance Method Details

#ascii_onlyObject



15
16
17
# File 'lib/powerpack/string/ascii_only.rb', line 15

def ascii_only
  dup.ascii_only!
end

#ascii_only!Object



32
33
34
35
36
37
38
39
40
# File 'lib/powerpack/string/ascii_only.rb', line 32

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?Boolean

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

Examples:

''.blank? #=> true
'    '.blank? #=> true
'  test'.blank? #=> false

Returns:

  • (Boolean)

    true is the string is blank, false otherwise



16
17
18
# File 'lib/powerpack/string/blank.rb', line 16

def blank?
  empty? || strip.empty?
end

#format(*args) ⇒ String

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

Examples:

'This is %s!'.format('Sparta') #=> 'This is Sparta!'
'My name is %{fname} %{lname}.'.format(fname: 'Bruce', lname: 'Wayne')
#=> 'My name is Bruce Wayne.'
'%d + %d'.format([1, 2]) #=> '1 + 2'

Returns:

  • (String)

    the formatted string



16
17
18
# File 'lib/powerpack/string/format.rb', line 16

def format(*args)
  super(self, *args.flatten(1))
end

#remove(pattern) ⇒ String

Removes all occurrences of a pattern in a string.

Returns:

  • (String)

    a new string without any occurrences of the pattern.



6
7
8
# File 'lib/powerpack/string/remove.rb', line 6

def remove(pattern)
  dup.remove!(pattern)
end

#remove!(pattern) ⇒ String

Removes all occurrences of a pattern in a string.

Returns:

  • (String)

    the string without any occurrences of the pattern.



13
14
15
# File 'lib/powerpack/string/remove.rb', line 13

def remove!(pattern)
  gsub!(pattern, '')
end

#remove_prefix(pattern) ⇒ String

Removes a prefix in a string.

Examples:

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

Returns:

  • (String)

    a new string without the prefix.



9
10
11
# File 'lib/powerpack/string/remove_prefix.rb', line 9

def remove_prefix(pattern)
  dup.remove_prefix!(pattern)
end

#remove_prefix!(pattern) ⇒ String

Removes a prefix in a string.

Examples:

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

Returns:

  • (String)

    the string without the prefix.



19
20
21
22
# File 'lib/powerpack/string/remove_prefix.rb', line 19

def remove_prefix!(pattern)
  gsub!(/\A#{pattern}/, '')
  self
end

#remove_suffix(pattern) ⇒ String

Removes a suffix in a string.

Examples:

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

Returns:

  • (String)

    a new string without the suffix.



9
10
11
# File 'lib/powerpack/string/remove_suffix.rb', line 9

def remove_suffix(pattern)
  dup.remove_suffix!(pattern)
end

#remove_suffix!(pattern) ⇒ String

Removes a suffix in a string.

Examples:

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

Returns:

  • (String)

    the string without the suffix.



19
20
21
22
# File 'lib/powerpack/string/remove_suffix.rb', line 19

def remove_suffix!(pattern)
  gsub!(/#{pattern}\z/, '')
  self
end

#squishString

Strips leading and trailing whitespace and squashes internal whitespace.

Examples:

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

Returns:

  • (String)

    a new string with no leading and trailing whitespace and no consecutive whitespace characters inside it



10
11
12
# File 'lib/powerpack/string/squish.rb', line 10

def squish
  dup.squish!
end

#squish!String

Strips leading and trailing whitespace and squashes internal whitespace.

Examples:

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

Returns:

  • (String)

    the string with no leading and trailing whitespace and no consecutive whitespace characters inside it



21
22
23
24
25
# File 'lib/powerpack/string/squish.rb', line 21

def squish!
  strip!
  gsub!(/\s+/, ' ')
  self
end

#strip_indentObject

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

Examples:


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

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


16
17
18
19
20
# 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) ⇒ Object

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

Examples:


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

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


16
17
18
19
# File 'lib/powerpack/string/strip_margin.rb', line 16

def strip_margin(margin_characters)
  margin = Regexp.quote(margin_characters)
  gsub(/^\s+#{margin}/, '')
end