Ruby: What Is the Easiest Way to Remove the First Element from an Array

Ruby: What is the easiest way to remove the first element from an array?

"pop"ing the first element of an Array is called "shift" ("unshift"
being the operation of adding one element
in front of the array).

How can I delete one element from an array by value

I think I've figured it out:

a = [3, 2, 4, 6, 3, 8]
a.delete(3)
#=> 3
a
#=> [2, 4, 6, 8]

Ruby Remove first index of an array

Use the Array#shift method, it does exactly what you want:

a = [1, 2, 3]
a.shift # => 1
a # => [2, 3]

Ruby: Remove first and last element of an Array - why the solution works one way & not the other

This is because both shift and pop return the value that is removed:

[1, 2, 3].pop   # => returns 3
[1, 2, 3].shift # => returns 1

So when you chain them together you're calling #pop on the result of #shift, which is an Integer which isn't allowed.

Delete first x entires of an array


I have an array of items, and I need to delete the first x items of it.

To non-destructive deletion

Array#drop(x) will do the work for you.

Drops first n elements from ary and returns the rest of the elements in an array.If a negative number is given, raises an ArgumentError.

my_items = [ 'item1', 'item2', 'item3', 'item4' ]
p my_items.drop(2)
p my_items

# >>["item3", "item4"]
# >>["item1", "item2", "item3", "item4"]

To destructive deletion

Array#shift

Removes the first element of self and returns it (shifting all other elements down by one). Returns nil if the array is empty.If a number n is given, returns an array of the first n elements (or less) just like array.slice!(0, n) does.

my_items = [ 'item1', 'item2', 'item3', 'item4' ]
my_items.shift(2)
p my_items # => ["item3", "item4"]

How to remove first element of each element

Ruby srtrings have a method which deletes a prefix:

value.map{|v| v.delete_prefix("$").to_f}
# => [6558.07, 468.95, 0.487526]

How do I remove the beginning elements of my array only if the first element satisfies a condition?

Sounds like you're trying to delete entities if they match their idx (provided the first idx is 0). Try this:

   if array.first == 0 
new_array = array.reject.each_with_index{ |item, idx| item == idx }
end

Although this will only work with ordered arrays of unique numbers, if you're not sure that they are then include: array = array.sort.uniq

Delete first instance of matching element from array


li.delete_at(li.index(n) || li.length)

li[li.length] is out of range, so the || li.length handles the case where n isn't in the list.

irb(main):001:0> li = [1,2,3,1,2,3]
=> [1, 2, 3, 1, 2, 3]
irb(main):002:0> li.delete_at(li.index(2) || li.length)
=> 2
irb(main):003:0> li.delete_at(li.index(42) || li.length)
=> nil
irb(main):004:0> li
=> [1, 3, 1, 2, 3]

ruby Remove one element from array

As @7urkm3n mentioned in the comments, you can use x.delete_at to delete the first occurance

x.delete_at(x.index 2)

> x = [1,2,3,2]
=> [1, 2, 3, 2]
> x.delete_at(x.index 2)
=> 2
> x
=> [1, 3, 2]

Ruby hash of arrays remove first element from array

One might shift a required element inplace.

arr.group_by &:shift

Whether you do not want to modify an original array:

arr.map(&:dup).group_by &:shift


Related Topics



Leave a reply



Submit