aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2015-03-01 19:05:47 -0800
committerDavid Heinemeier Hansson <david@loudthinking.com>2015-03-01 19:05:47 -0800
commitfc91616a4e6c953c419cccbce2ec7a3e1c0e3941 (patch)
tree52b368ecc24bde879cedaebf75783d4192b83d3a /activesupport/lib
parent3a156ec8e79b88ff90e8f5ed34dd05b89e94b72f (diff)
parentc7a37c10ade8372c712be01a7e4ddbe8785c5139 (diff)
downloadrails-fc91616a4e6c953c419cccbce2ec7a3e1c0e3941.tar.gz
rails-fc91616a4e6c953c419cccbce2ec7a3e1c0e3941.tar.bz2
rails-fc91616a4e6c953c419cccbce2ec7a3e1c0e3941.zip
Merge pull request #19157 from todd/enumerable_without
Add Enumerable#without
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/core_ext/array/grouping.rb12
-rw-r--r--activesupport/lib/active_support/core_ext/enumerable.rb11
2 files changed, 23 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/array/grouping.rb b/activesupport/lib/active_support/core_ext/array/grouping.rb
index 87ae052eb0..b3ada47880 100644
--- a/activesupport/lib/active_support/core_ext/array/grouping.rb
+++ b/activesupport/lib/active_support/core_ext/array/grouping.rb
@@ -113,4 +113,16 @@ class Array
results
end
end
+
+ # Returns a copy of the Array without the specified elements.
+ #
+ # people = ["David", "Rafael", "Aaron", "Todd"]
+ # people.without "Aaron", "Todd"
+ # => ["David", "Rafael"]
+ #
+ # Note: This is an optimization of `Enumerable#without` that uses `Array#-`
+ # instead of `Array#reject` for performance reasons.
+ def without(*elements)
+ self - elements
+ end
end
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb
index 1343beb87a..d699cf4241 100644
--- a/activesupport/lib/active_support/core_ext/enumerable.rb
+++ b/activesupport/lib/active_support/core_ext/enumerable.rb
@@ -60,6 +60,17 @@ module Enumerable
def exclude?(object)
!include?(object)
end
+
+ # Returns a copy of the enumerable without the specified elements.
+ #
+ # ["David", "Rafael", "Aaron", "Todd"].without "Aaron", "Todd"
+ # => ["David", "Rafael"]
+ #
+ # {foo: 1, bar: 2, baz: 3}.without :bar
+ # => {foo: 1, baz: 3}
+ def without(*elements)
+ reject { |element| element.in?(elements) }
+ end
end
class Range #:nodoc: