forked from rubyworks/facets
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Array#reject_values. (smackesey)
- Loading branch information
Showing
3 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Array | ||
|
||
# Non-destructive form of `Array#delete_values`. Unlike `delete_values` | ||
# this method returns a new array. | ||
# | ||
# values - List of array elements to reject. | ||
# | ||
# Examples | ||
# | ||
# [1,2,3,4,5].reject_values(2,4) # => [1,3,5] | ||
# | ||
# Returns [Array] | ||
# | ||
# CREDIT: Sean Mackesey | ||
|
||
def reject_values(*values) | ||
reject { |x| values.include?(x) } | ||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
covers 'facets/array/reject_values' | ||
|
||
testcase Array do | ||
|
||
method :reject_values do | ||
|
||
test do | ||
a = [1,2,3,4,5] | ||
r = a.reject_values(2,4) | ||
r.assert == [1,3,5] | ||
a.assert = [1,2,3,4,5] # hasn't changed | ||
end | ||
|
||
end | ||
|
||
end | ||
|