Class: String
- Inherits:
-
Object
- Object
- String
- 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
- #ascii_only ⇒ Object
- #ascii_only! ⇒ Object
-
#blank? ⇒ Boolean
Checks whether a string is blank.
-
#format(*args) ⇒ String
A nicer alternative to Kernel#sprintf and String#%.
-
#remove(pattern) ⇒ String
Removes all occurrences of a pattern in a string.
-
#remove!(pattern) ⇒ String
Removes all occurrences of a pattern in a string.
-
#remove_prefix(pattern) ⇒ String
Removes a prefix in a string.
-
#remove_prefix!(pattern) ⇒ String
Removes a prefix in a string.
-
#remove_suffix(pattern) ⇒ String
Removes a suffix in a string.
-
#remove_suffix!(pattern) ⇒ String
Removes a suffix in a string.
-
#squish ⇒ String
Strips leading and trailing whitespace and squashes internal whitespace.
-
#squish! ⇒ String
Strips leading and trailing whitespace and squashes internal whitespace.
-
#strip_indent ⇒ Object
The method strips the whitespace preceding the base indentation.
-
#strip_margin(margin_characters) ⇒ Object
The method strips the characters preceding a special margin character.
Instance Method Details
#ascii_only ⇒ Object
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! = { :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'), ** end |
#blank? ⇒ Boolean
Checks whether a string is blank. A string is considered blank if it is either empty or contains only whitespace characters.
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#%.
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.
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.
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.
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.
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.
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.
19 20 21 22 |
# File 'lib/powerpack/string/remove_suffix.rb', line 19 def remove_suffix!(pattern) gsub!(/#{pattern}\z/, '') self end |
#squish ⇒ String
Strips leading and trailing whitespace and squashes internal whitespace.
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.
21 22 23 24 25 |
# File 'lib/powerpack/string/squish.rb', line 21 def squish! strip! gsub!(/\s+/, ' ') self end |
#strip_indent ⇒ Object
The method strips the whitespace preceding the base indentation. Useful for HEREDOCs and other multi-line strings.
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.
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 |