Class: String

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

Overview

# encoding: utf-8

Instance Method Summary (collapse)

Instance Method Details

- (Object) ascii_only



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

def ascii_only
  dup.ascii_only!
end

- (Object) ascii_only!



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

- (Boolean) blank?

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


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

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

- (String) format(*args)

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'


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

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

- (String) remove(pattern)

Removes all occurrences of a pattern in a string.



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

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

- (String) remove!(pattern)

Removes all occurrences of a pattern in a string.



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

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

- (String) remove_prefix(pattern)

Removes a prefix in a string.

Examples:

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


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

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

- (String) remove_prefix!(pattern)

Removes a prefix in a string.

Examples:

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


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

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

- (String) remove_suffix(pattern)

Removes a suffix in a string.

Examples:

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


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

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

- (String) remove_suffix!(pattern)

Removes a suffix in a string.

Examples:

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


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

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

- (String) squish

Strips leading and trailing whitespace and squashes internal whitespace.

Examples:

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


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

def squish
  dup.squish!
end

- (String) squish!

Strips leading and trailing whitespace and squashes internal whitespace.

Examples:

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


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

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

- (Object) strip_indent

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

- (Object) strip_margin(margin_characters)

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