Ruby: ¿Cuál es la forma más fácil de eliminar el primer elemento de un array?
Digamos que tengo una matriz
[0, 132, 432, 342, 234]
¿Cuál es la forma más fácil de deshacerse del primer elemento? (0)
11 answers
"pop"ing el primer elemento de un Array se llama" shift "("unshift" siendo la operación de agregar un elemento delante de la matriz).
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-26 11:15:33
a = [0,1,2,3]
a.drop(1)
# => [1, 2, 3]
a
# => [0,1,2,3]
Y adicionalmente:
[0,1,2,3].drop(2)
=> [2, 3]
[0,1,2,3].drop(3)
=> [3]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-23 01:48:34
Utilice el método shift
en array
>> x = [4,5,6]
=> [4, 5, 6]
>> x.shift
=> 4
>> x
=> [5, 6]
Si desea eliminar n elementos iniciales, puede usar x.shift(n)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-01 07:07:58
[0, 132, 432, 342, 234][1..-1]
=> [132, 432, 342, 234]
Así que a diferencia de shift
o slice
esto devuelve el array modificado (útil para un liners).
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-01 08:33:00
Esto es bastante limpio:
head, *tail = [1, 2, 3, 4, 5]
#==> head = 1, tail = [2, 3, 4, 5]
Como está escrito en los comentarios, hay una ventaja de no mutar la lista original.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-09 08:22:50
O a.delete_at 0
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-01 12:25:51
Use el método shift
array.shift(n) => Remove first n elements from array
array.shift(1) => Remove first element
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-19 11:16:22
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-01 07:06:51
Puede usar Array.delete_at (0) método que eliminará el primer elemento.
x = [2,3,4,11,0]
x.delete_at(0) unless x.empty? # [3,4,11,0]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-06 11:13:38
Puedes usar: arr - [arr[0]]
o de lo contrario
arr - [arr.shift()]
o simplemente
arr.shift(1)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-07 07:32:09
Puedes usar:
a.delete(a[0])
a.delete_at 0
Ambos pueden funcionar
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-14 07:07:39