Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add String#remove{,!}

  • Loading branch information...
commit dbc1b9c75790b2db545a16ee91c4f26a50482420 1 parent d72b6b0
@gsamokovarov gsamokovarov authored
View
2  CHANGELOG.md
@@ -2,6 +2,8 @@
## master (unreleased)
+* Added `String#remove`
+* Added `String#remove!`
* Added `String#ascii_only`
* Added `String#ascii_only!`
View
1  lib/powerpack/string.rb
@@ -1,6 +1,7 @@
require_relative 'string/ascii_only'
require_relative 'string/blank'
require_relative 'string/format'
+require_relative 'string/remove'
require_relative 'string/strip_indent'
require_relative 'string/strip_margin'
require_relative 'string/squish'
View
17 lib/powerpack/string/remove.rb
@@ -0,0 +1,17 @@
+unless String.method_defined? :remove
+ class String
+ # Removes all occurrences of a pattern in a string.
+ #
+ # @return [String] a new string without any occurrences of the pattern.
+ def remove(pattern)
+ dup.remove!(pattern)
+ end
+
+ # Removes all occurrences of a pattern in a string.
+ #
+ # @return [String] the string without any occurrences of the pattern.
+ def remove!(pattern)
+ gsub!(pattern, '')
+ end
+ end
+end
View
13 spec/powerpack/string/remove_spec.rb
@@ -0,0 +1,13 @@
+require 'spec_helper'
+
+describe 'String#remove' do
+ it 'removes all occurrences of a pattern' do
+ expect('Ladies Night'.remove(/Ladies /)).to eq('Night')
+ end
+end
+
+describe 'String#remove!' do
+ it 'removes all occurrences of a pattern' do
+ expect('Ladies Night'.remove(/Ladies /)).to eq('Night')
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.