aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/array
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2019-03-05 16:04:41 -0800
committerDavid Heinemeier Hansson <david@loudthinking.com>2019-03-05 16:04:41 -0800
commitbfaa3091c3c32b5980a614ef0f7b39cbf83f6db3 (patch)
tree040de4c20837ba49faf87ac9f7251f4df0afb9af /activesupport/lib/active_support/core_ext/array
parent91ed21b304c468db8ce9fd830312c151432935d0 (diff)
downloadrails-bfaa3091c3c32b5980a614ef0f7b39cbf83f6db3.tar.gz
rails-bfaa3091c3c32b5980a614ef0f7b39cbf83f6db3.tar.bz2
rails-bfaa3091c3c32b5980a614ef0f7b39cbf83f6db3.zip
Added Array#including, Array#excluding, Enumerable#including, Enumerable#excluding
Diffstat (limited to 'activesupport/lib/active_support/core_ext/array')
-rw-r--r--activesupport/lib/active_support/core_ext/array/access.rb20
1 files changed, 16 insertions, 4 deletions
diff --git a/activesupport/lib/active_support/core_ext/array/access.rb b/activesupport/lib/active_support/core_ext/array/access.rb
index b7ff7a3907..c9eecc55f9 100644
--- a/activesupport/lib/active_support/core_ext/array/access.rb
+++ b/activesupport/lib/active_support/core_ext/array/access.rb
@@ -29,16 +29,28 @@ class Array
end
end
- # Returns a copy of the Array without the specified elements.
+ # Returns a new array that includes the passed elements.
+ #
+ # Example: [ 1, 2, 3 ].including(4, 5) => [ 1, 2, 3, 4, 5 ]
+ def including(*elements)
+ self + elements.flatten
+ end
+
+ # Returns a copy of the Array excluding the specified elements.
#
# people = ["David", "Rafael", "Aaron", "Todd"]
- # people.without "Aaron", "Todd"
+ # people.excluding "Aaron", "Todd"
# # => ["David", "Rafael"]
#
- # Note: This is an optimization of <tt>Enumerable#without</tt> that uses <tt>Array#-</tt>
+ # Note: This is an optimization of <tt>Enumerable#excluding</tt> that uses <tt>Array#-</tt>
# instead of <tt>Array#reject</tt> for performance reasons.
+ def excluding(*elements)
+ self - elements.flatten
+ end
+
+ # Alias for #excluding.
def without(*elements)
- self - elements
+ excluding(*elements)
end
# Equal to <tt>self[1]</tt>.