Module: Corefines::String::Remove

Defined in:
lib/corefines/string.rb

Instance Method Summary collapse

Instance Method Details

#remove(*patterns) ⇒ String

Returns a copy of this string with all occurrences of the patterns removed.

The pattern is typically a Regexp; if given as a String, any regular expression metacharacters it contains will be interpreted literally, e.g. '\d' will match a backlash followed by 'd', instead of a digit.

Examples:

str = "This is a good day to die"
str.remove(" to die") # => "This is a good day"
str.remove(/\s*to.*$/) # => "This is a good day"
str.remove("to die", /\s*$/) # => "This is a good day"

Parameters:

  • *patterns (Regexp, String)

    patterns to remove from the string.

Returns:


335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/corefines/string.rb', line 335

module Remove
  refine ::String do
    def remove(*patterns)
      dup.remove!(*patterns)
    end

    def remove!(*patterns)
      patterns.each { |pattern| gsub!(pattern, '') }
      self
    end
  end
end

#remove!(*patterns) ⇒ String

Removes all the occurrences of the patterns in place.

Parameters:

  • *patterns (Regexp, String)

    patterns to remove from the string.

Returns:

See Also:


335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/corefines/string.rb', line 335

module Remove
  refine ::String do
    def remove(*patterns)
      dup.remove!(*patterns)
    end

    def remove!(*patterns)
      patterns.each { |pattern| gsub!(pattern, '') }
      self
    end
  end
end