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
# 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>.
#
# %w( a b c d e ).second # => "b"

View File

@ -113,16 +113,4 @@ 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

View File

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