Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Enumerable#except #133

Closed
wants to merge 1 commit into from

2 participants

Burke Libbey Aaron Patterson
Burke Libbey

I realize there are plenty of good reasons not to further pollute Enumerable, but I get a lot of use out of this method, and I think other people would find it handy as well.

Aaron Patterson
Owner
irb(main):001:0> [1,2,3,4,5] - [2,4]
=> [1, 3, 5]
irb(main):002:0> 
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 13, 2010
  1. Added Enumerable#except

    Burke Libbey authored
This page is out of date. Refresh to see the latest.
10 activesupport/lib/active_support/core_ext/enumerable.rb
View
@@ -104,6 +104,16 @@ def many?(&block)
def exclude?(object)
!include?(object)
end
+
+ # Return a version of the collection with the provided elements removed. Similar to subtracting an array.
+ #
+ # [1,2,3,4,5].except(2,4)
+ # => [1,3,5]
+ #
+ def except(*exclusions)
+ reject { |e| exclusions.include?(e) }
+ end
+
end
class Range #:nodoc:
7 activesupport/test/core_ext/enumerable_test.rb
View
@@ -94,4 +94,11 @@ def test_exclude?
assert [ 1 ].exclude?(2)
assert ![ 1 ].exclude?(1)
end
+
+ def test_except
+ assert_equal([1, 2, 3, 4, 5].except(2, 4), [1, 3, 5])
+ assert_equal([:a, :b, :c].except(:a), [:b, :c])
+ assert_equal([:a, :b, :c].except(nil), [:a, :b, :c])
+ end
+
end
Something went wrong with that request. Please try again.