Is there a built-in method which performs the same function as Array#delete
but returns self
? I'd like to do it without using a block and clearer than an_ary.-([el])
.
I could monkeypatch one, but it seems like a "compact with arguments" method would be a relatively common desire?
An array is a data structure, which can store a fixed-size collection of elements of the same data type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
An array is a collection of similar types of data. For example, if we want to store the names of 100 people then we can create an array of the string type that can store 100 names. String[] array = new String[100];
There are three different kinds of arrays: indexed arrays, multidimensional arrays, and associative arrays.
An array type is a user-defined data type consisting of an ordered set of elements of a single data type. An ordinary array type has a defined upper bound on the number of elements and uses the ordinal position as the array index.
If you want to mutate the original array, like delete
, here are options:
ary.reject!{|e| e==42 }.something_else
ary.tap{|a| a.delete 42}.something_else
(ary.delete 42;ary).something_else
(ary-=[42]).something_else
If you want a new array to chain from:
ary.reject{|e| e==42 }.something_else
(ary-[42]).something_else
an_ary.-([el])
looks awful.
What about...
an_ary - [el]
?
The most natural way of dealing with mathematical operations is this...
4 - 2
Not this...
4.-(2)
array.reject{|element| element == value_of_element_to_be_deleted}
You can do
my_array.first(n) #1
my_array.last(n) #2
If the elements of the array you want to delete, are at the end (1) or at the beginning (2) of the array.
I had this same question for Array#delete_at
that returned an array with the element at a specified index removed, which is why I ended up here. Looks like it isn't built in. In case anyone else is interested, I quickly wrote this monkey patch (I gave this virtually no thought regarding efficiency, and I originally didn't handle any cases such as negative indices or out of bounds indices...but then I decided to quick throw a couple in there):
class Array
def remove_at(i)
# handle index out of bounds by returning unchanged array
return self if i >= self.length
# remove the i-th element from the end if i is negative
if i < 0
i += self.length
# handle index out of bounds by returning unchanged array
return self if i < 0
end
# Return an array composed of the elements before the specified
# index plus the elements after the specified index
return self.first(i) + self.last(self.length - i - 1)
end
end
test = [0,1,2,3,4,5]
puts test.remove_at(3).inspect
puts test.remove_at(5).inspect
puts test.remove_at(6).inspect
puts test.remove_at(-7).inspect
puts test.remove_at(-2).inspect
I had fun whipping this up, so I figured I might as well post it here :)
I prefer this way:
list = [1, 2, 3, 4, 5]
list.tap { |list| list.delete(2) } # => [1, 3, 4, 5]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With