ruby 101 session 2
DESCRIPTION
Parte 2 de 5 del curso de RubyTRANSCRIPT
![Page 1: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/1.jpg)
ARREGLOS, HASHES Y MÉTODOS
Ruby 101
Sergio Castillo Yrizales@scyrizales
![Page 2: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/2.jpg)
Agenda
Arreglos
Hashes
Métodos
![Page 3: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/3.jpg)
Arreglos
![Page 4: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/4.jpg)
Arreglos
Estructura de datos que permite almacenar varios valores en una sola variable
![Page 5: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/5.jpg)
Iterando sobre arreglos
Se usa el comando .each para recorrer arreglos
![Page 6: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/6.jpg)
.times
Sirve para repetir un bloque cierto numero de veces
![Page 7: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/7.jpg)
Arreglos de Arreglos
Los elementos de un arreglo… pueden ser otros arreglos
![Page 8: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/8.jpg)
Iterando sobre Arreglos Multidimensionales
Usando doble .each
![Page 9: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/9.jpg)
Hashes
![Page 10: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/10.jpg)
¿Qué es un Hash?
Los arreglos se indexan por números
Se accesan a ellos con la sintaxis
Los hashes se indexan por cualquier dato
![Page 11: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/11.jpg)
Creando un Hash
En otros lenguajes los pueden encontrar como diccionarios
![Page 12: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/12.jpg)
Creando un Hash II
Tambien se pueden crear asi:
Y le agregamos datos de esta forma:
Y recuperamos la data asi:
![Page 13: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/13.jpg)
Hashes. Valor por defecto
El valor que se usa en el constructor será el valor por defecto
![Page 14: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/14.jpg)
Iterando sobre Hashes
Con el comando each y la sintaxis |key, value|
![Page 15: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/15.jpg)
Ordenando Hashes
El ordenamiento se puede realizar por key o por value:
Donde a es “key” y b es “value”
![Page 16: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/16.jpg)
Reto: Contemos palabras
Construiremos un histograma que nos ayude a contar cuantas veces se repiten las palabras en un texto.
Si ingresamos: “La lluvia que cayo en Lima mojo la calle” debe retornar
![Page 17: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/17.jpg)
Métodos
![Page 18: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/18.jpg)
Como crear un metodo
De la siguiente forma
** es potencia
![Page 19: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/19.jpg)
return
Sirve para devolver un valor
Ambas son equivalentes
Ruby siempre devolvera el resultado de la última expresión que evalue dentro del método
![Page 20: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/20.jpg)
splats
Cantidad indefinida de parametrosEl último tiene que tener *Se comporta como un arreglo
![Page 21: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/21.jpg)
Ordenamiento
Usamos el metodo sort para ordenar un arreglo
Podemos usar <=> para comparar -1 si el primero es mayor, 0 iguales, 1 segundo mayor
![Page 22: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/22.jpg)
Ordenemos la biblioteca
Ingresaremos una lista de libros
Definimos un metodo para ordenarlos
Mediante un parametro debemos poder ordenar ascendente o descendente
![Page 23: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/23.jpg)
Materiales de esta sesión
Los pueden encontrar en: https://github.com/scyrizales/Ruby101/tree/master/Se
gundaClase
![Page 24: Ruby 101 session 2](https://reader030.vdocument.in/reader030/viewer/2022012902/55853c09d8b42a4f018b483b/html5/thumbnails/24.jpg)
Muchas gracias