Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array#delete, but return the array?

Tags:

ruby

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?

like image 274
wersimmon Avatar asked Apr 06 '11 20:04

wersimmon


People also ask

What do you mean by array?

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.

What is an array with example?

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];

What are the 3 types of arrays?

There are three different kinds of arrays: indexed arrays, multidimensional arrays, and associative arrays.

What is array and its type?

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.


6 Answers

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
like image 101
Phrogz Avatar answered Oct 15 '22 05:10

Phrogz


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)
like image 35
Victor Avatar answered Oct 15 '22 05:10

Victor


array.reject{|element| element == value_of_element_to_be_deleted}
like image 44
steven_noble Avatar answered Oct 15 '22 05:10

steven_noble


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.

like image 36
juliangonzalez Avatar answered Oct 15 '22 05:10

juliangonzalez


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 :)

like image 38
MuffinTheMan Avatar answered Oct 15 '22 06:10

MuffinTheMan


I prefer this way:

list = [1, 2, 3, 4, 5]
list.tap { |list| list.delete(2) } # => [1, 3, 4, 5]
like image 31
Andres Leon Avatar answered Oct 15 '22 07:10

Andres Leon