Improve Spanish translation for Ruby's guide (#29076)

This commit is contained in:
Ignacio Capuccio
2019-08-13 10:47:45 -03:00
committed by Randell Dawson
parent d1713d3574
commit c2bbde95a3

View File

@ -1,144 +1,217 @@
--- ---
title: Common Array Methods title: Common Array Methods
localeTitle: Métodos comunes de matriz localeTitle: Métodos comunes de Array
--- ---
## Métodos comunes de matriz ## Métodos comunes de Array
Ruby Arrays forma una base fundamental en la programación en Ruby, y la mayoría de los lenguajes de hecho. Se utiliza tanto que sería beneficioso conocer e incluso memorizar algunos de los métodos más utilizados para los arreglos. Si quieres saber más sobre Ruby Arrays, tenemos [un artículo sobre ellos](https://guide.freecodecamp.org/ruby/ruby-arrays) . Los Arrays (arreglos) de Ruby forman una base fundamental en la programación en Ruby, y en la mayoría de los lenguajes de hecho. Se utilizan tanto que sería beneficioso conocer e incluso memorizar algunos de los métodos más utilizados para arrays. Si quieres saber más sobre los Arrays de Ruby, tenemos [un artículo sobre ellos](https://guide.freecodecamp.org/ruby/ruby-arrays).
Para los fines de esta guía, nuestra matriz será la siguiente: Para los fines de esta guía, nuestro array será el siguiente:
\`\` \`rubí array = \[0, 1, 2, 3, 4\] ``` ruby
array = [0, 1, 2, 3, 4]
``` ```
#### .length #### .length
The .length method tallies the number of elements in your array and returns the count: El método .length calcula el número de elementos en tu array y retorna la cuenta:
``` ruby
array.length
=> 5
```
Este método es similar a los métodos .count y .size.
``` ruby
array.count
=> 5
```
``` ruby
array.size
=> 5
``` ```
rubí array.length => 5 #### .first
``` El método .first accede al primer elemento del array, es decir el elemento en el índice 0:
#### .first
The .first method accesses the first element of the array, the element at index 0: ``` ruby
array.first
=> 0
``` ```
rubí array.first => 0 #### .last
``` El método .last accede al último elemento del array:
#### .last
The .last method accesses the last element of the array: ``` ruby
array.last
=> 4
``` ```
rubí array.last => 4 #### .take
``` El método .take retorna los primeros n elementos del array:
#### .take
The .take method returns the first n elements of the array: ``` ruby
array.take(3)
=> [0, 1, 2]
``` ```
rubí array.take (3) => \[0, 1, 2\] #### .drop
``` El método .drop retorna los elementos siguientes después de los primeros n elementos del array:
#### .drop
The .drop method returns the elements after n elements of the array: ``` ruby
array.drop(3)
=> [3, 4]
``` ```
rubí array.drop (3) => \[3, 4\] #### índice del array
``` Puedes acceder a un elemento determinado en el array a través de su índice. Si el índice no existe en el array, se retornará nulo:
#### array index
You can access a specific element in an array by accessing its index. If the index does not exist in the array, nil will be returned: ```ruby
array[2]
=> 2
array[5]
=> nil
``` ```
rubí array \[2\] => 2 #### .pop
El método .pop removerá de forma permanente el último elemento del array:
array \[5\] => nil ``` ruby
``` array.pop
#### .pop => [0, 1, 2, 3]
The .pop method will permantently remove the last element of an array:
``` ```
rubí array.pop => \[0, 1, 2, 3\] #### .shift
``` El método .shift removerá de forma permanente el primer elemento del array y retornará este elemento:
#### .shift
The .shift method will permantently remove the first element of an array and return this element: ``` ruby
array.shift
=> 0
array
=> [1, 2, 3, 4]
``` ```
rubí array.shift => 0 #### .push
formación => \[1, 2, 3, 4\] El método .push te permitirá agregar un elemento al final del array:
```
#### .push ``` ruby
The .push method will allow you to add an element to the end of an array: array.push(99)
=> [0, 1, 2, 3, 4, 99]
``` ```
rubí array.push (99) => \[0, 1, 2, 3, 4, 99\] #### .unshift
El método .unshift te permitirá agregar un elemento al principio del array:
``` ```
#### .unshift array = [2, 3]
The .unshift method will allow you to add an element to the beginning of an array: array.unshift(1)
=> [1, 2, 3]
``` ```
array = \[2, 3\] array.unshift (1) => \[1, 2, 3\] #### .delete
``` El método .delete remueve un elemento determinado de un array de forma permanente:
#### .delete
The .delete method removes a specified element from an array permanently: ``` ruby
array.delete(1)
=> [0, 2, 3, 4]
``` ```
rubí array.delete (1) => \[0, 2, 3, 4\] #### .delete_at
``` El método .delete_at te permite remover de forma permanente un elemento del array para un índice determinado:
#### .delete_at
The .delete_at method allows you to permanently remove an element of an array at a specified index: ``` ruby
array.delete_at(0)
=> [1, 2, 3, 4]
``` ```
rubí array.delete\_at (0) => \[1, 2, 3, 4\] #### .reverse
``` El método .reverse retorna un nuevo array con los mismos elementos del array original, pero con el orden invertido:
#### .reverse
The .reverse method reverses the array but does not mutate it (the original array stays as is): ``` ruby
array.reverse
=> [4, 3, 2, 1, 0]
array
=> [0, 1, 2, 3, 4]
``` ```
rubí array.reverse => \[4, 3, 2, 1, 0\] #### .select
``` El método .select itera sobre un array y retorna un nuevo array que incluye cualquier item que retorne verdadero a la expresión provista:
#### .select
The .select method iterates over an array and returns a new array that includes any items that return true to the expression provided. ``` ruby
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
array.select { |number| number > 4 }
=> [5, 6, 7, 8, 9, 10]
array
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
``` ```
rubí array = \[1, 2, 3, 4, 5, 6, 7, 8, 9, 10\] array.select {| number | número> 4} => \[5, 6, 7, 8, 9, 10\] formación => \[5, 6, 7, 8, 9, 10\] #### .include?
``` El método include? verifica si el argumento dado está incluido en el array:
#### .include?
The include? method checks to see if the argument given is included in the array: ``` ruby
array = [1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]
array.include?(3)
=> true
``` ```
rubí array = \[1, 2, 3, 4, 5\] => \[1, 2, 3, 4, 5\] array.include? (3) => verdadero #### .flatten
#### .aplanar El método .flatten se puede usar para tomar un array que contiene arrays anidados y crear uno nuevo de una sola dimensión:
El método de aplanamiento se puede usar para tomar una matriz que contiene matrices anidadas y crear una matriz unidimensional: ``` ruby
array = [1, 2, [3, 4, 5], [6, 7]]
\`\` \`rubí array = \[1, 2, \[3, 4, 5\], \[6, 7\]\] array.flatten => \[1, 2, 3, 4, 5, 6, 7\] array.flatten
``` => [1, 2, 3, 4, 5, 6, 7]
#### .join
The .join method returns a string of all the elements of the array separated by a separator parameter. If the separator parameter is nil, the method uses an empty string as a separator between strings.
``` ```
rubí array.join => "1234" array.join (" _") => "1_ 2 _3_ 4" #### .join
``` El método .join retorna una cadena con todos los elementos del array separados por un parámetro separador. Si el parámetro separador es nulo, el método usa una cadena vacía como separador:
#### .each
The .each method iterates over each element of the array, allowing you to perform actions on them. ``` ruby
array.join
=> "01234"
array.join("*")
=> "0*1*2*3*4"
``` ```
rubí array.each do | elemento | pone elemento fin => 0 1 2 3 4 #### .each
``` El método .each itera sobre cada elemento del array, permitiéndote ejecutar acciones sobre ellos:
#### .map
The .map method is the same as the .collect method. The .map and .collect methods iterate over each element of the array, allowing you to perform actions on them. The .map and .collect methods differ from the .each method in that they return an array containing the transformed elements. ``` ruby
array.each { |element| puts element }
=>
0
1
2
3
4
``` ```
rubí array.map {| element | elemento \* 2} pone elemento fin => 0 2 4 6 8 #### .map
``` El método .map es igual que el método .collect. Los métodos .map y .collect iteran sobre cada elemento del array, permitiéndote ejecutar acciones sobre ellos. Los métodos .map y .collect se diferencian del método .each en que éstos retornan un array que contiene los elementos transformados:
#### .uniq
The .uniq method takes in an array containing duplicate elements, and returns a copy of the array containing only unique elements--any duplicate elements are removed from the array. ``` ruby
array.map { |element| element * 2 }
=> [0, 2, 4, 6, 8]
``` ```
rubí array = \[1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 6, 7, 8\] array.uniq => \[1, 2, 3, 4, 5, 6, 7, 8\] #### .uniq
``` El método .uniq toma un array que contiene elementos duplicados y retorna una copia del array conteniendo solo los elementos únicos. Cualquier elemento duplicado es removido de este array:
#### .concat
The .concat method appends the elements from an array to the original array. The .concat method can take in multiple arrays as an argument, which will in turn append multiple arrays to the original array. ``` ruby
array = [1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 6, 7, 8]
array.uniq
=> [1, 2, 3, 4, 5, 6, 7, 8]
``` ```
rubí array = \[0, 1, 2, 3, 4\] array.concat (\[5, 6, 7\], \[8, 9, 10\]) => \[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10\] \`\` \` #### .concat
El método .concat concatena los elementos de un array provisto con los del array original. El método .concat puede tomar múliples arrays como argumento, lo cual concatenará múltiples arrays al array original:
``` ruby
array = [0, 1, 2, 3, 4]
array.concat([5, 6, 7], [8, 9, 10])
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
```
## Más información ## Más información
* [Documentos de Ruby Array](http://ruby-doc.org/core-2.5.1/Array.html)
* [Documentos de Ruby Array](http://ruby-doc.org/core-2.5.1/Array.html)