1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Move Array#without from Grouping to Access concern and add dedicated test (relates to #19157)

This commit is contained in:
David Heinemeier Hansson 2015-03-02 08:46:20 -08:00
parent 3f4964299a
commit 521318333e
3 changed files with 16 additions and 12 deletions

View file

@ -27,6 +27,18 @@ class Array
end end
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
# Equal to <tt>self[1]</tt>. # Equal to <tt>self[1]</tt>.
# #
# %w( a b c d e ).second # => "b" # %w( a b c d e ).second # => "b"

View file

@ -113,16 +113,4 @@ class Array
results results
end end
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 end

View file

@ -27,4 +27,8 @@ class AccessTest < ActiveSupport::TestCase
assert_equal array[4], array.fifth assert_equal array[4], array.fifth
assert_equal array[41], array.forty_two assert_equal array[41], array.forty_two
end end
def test_without
assert_equal [1, 2, 4], [1, 2, 3, 4, 5].without(3, 5)
end
end end