class String
Public Instance Methods
Source
# File lib/powerpack/string/ascii_only.rb, line 15 def ascii_only dup.ascii_only! end
Source
# 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
Source
# File lib/powerpack/string/blank.rb, line 16 def blank? empty? || strip.empty? end
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
Source
# File lib/powerpack/string/format.rb, line 16 def format(*args) super(self, *args.flatten(1)) end
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'
Source
# File lib/powerpack/string/remove.rb, line 6 def remove(pattern) dup.remove!(pattern) end
Removes all occurrences of a pattern in a string.
@return [String] a new string without any occurrences of the pattern.
Source
# File lib/powerpack/string/remove.rb, line 13 def remove!(pattern) gsub!(pattern, '') end
Removes all occurrences of a pattern in a string.
@return [String] the string without any occurrences of the pattern.
Source
# File lib/powerpack/string/remove_prefix.rb, line 9 def remove_prefix(pattern) dup.remove_prefix!(pattern) end
Removes a prefix in a string.
@return [String] a new string without the prefix.
@example
'Ladies Night'.remove_prefix('Ladies ') #=> 'Night'
Source
# File lib/powerpack/string/remove_prefix.rb, line 19 def remove_prefix!(pattern) gsub!(/\A#{pattern}/, '') self end
Removes a prefix in a string.
@return [String] the string without the prefix.
@example
'Ladies Night'.remove_prefix!('Ladies ') #=> 'Night'
Source
# File lib/powerpack/string/remove_suffix.rb, line 9 def remove_suffix(pattern) dup.remove_suffix!(pattern) end
Removes a suffix in a string.
@return [String] a new string without the suffix.
@example
'Ladies Night'.remove_suffix(' Night') #=> 'Ladies'
Source
# File lib/powerpack/string/remove_suffix.rb, line 19 def remove_suffix!(pattern) gsub!(/#{pattern}\z/, '') self end
Removes a suffix in a string.
@return [String] the string without the suffix.
@example
'Ladies Night'.remove_suffix!(' Night') #=> 'Ladies'
Source
# File lib/powerpack/string/squish.rb, line 10 def squish dup.squish! end
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'
Source
# File lib/powerpack/string/squish.rb, line 21 def squish! strip! gsub!(/\s+/, ' ') self end
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'
Source
# 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
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"
Source
# File lib/powerpack/string/strip_margin.rb, line 16 def strip_margin(margin_characters) margin = Regexp.quote(margin_characters) gsub(/^\s+#{margin}/, '') end
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"