Created
May 11, 2023 21:21
-
-
Save jjsantos01/2b3ad969581fce5099424d27ac9ca96e to your computer and use it in GitHub Desktop.
My notes from the Basic Ruby course in CodigoFacilito
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "71c07a77-4c64-42c1-89bc-b896b4246195", | |
"metadata": {}, | |
"source": [ | |
"# Impresión en terminal" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "326cf5bd-7998-4e0f-8034-5c4367e1296c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hola mundo, esto es Ruby\n" | |
] | |
} | |
], | |
"source": [ | |
"puts \"Hola mundo, esto es Ruby\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "9874b903-c73b-46f9-b573-04990ea87a51", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"En terminal ejecutaríamos: ruby hola_mundo.rb\n" | |
] | |
} | |
], | |
"source": [ | |
"puts \"En terminal ejecutaríamos: ruby hola_mundo.rb\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "b591d375-1fff-48b1-8ccb-155954f7b498", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Juan\n" | |
] | |
} | |
], | |
"source": [ | |
"nombre = \"Juan\"\n", | |
"puts nombre" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "2161c4bd-270e-4582-8137-4243d9d911b7", | |
"metadata": {}, | |
"source": [ | |
"* También existe `print()`. Se diferencia de `puts` porque este último añade un salto de línea al final, pero `print` no" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "47f6e133-dc22-4b3c-be78-54e457049f1e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"JuanSantos" | |
] | |
} | |
], | |
"source": [ | |
"print \"Juan\"\n", | |
"print \"Santos\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "b8088ed0-103f-4747-aca4-416661045623", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Juan\n", | |
"Santos\n" | |
] | |
} | |
], | |
"source": [ | |
"puts \"Juan\"\n", | |
"puts \"Santos\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "2ee0108f-5a47-4b03-8b2d-c98597ca2088", | |
"metadata": {}, | |
"source": [ | |
"# Números" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "697cd51c-9600-45e8-a00e-056eed56b700", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"40.0" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"20 * 2.0" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"id": "a361d3e6-c89e-4afa-b0e6-0d0054edd563", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# hace división entero entre enteros\n", | |
"10 / 3" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"id": "232fd9e8-abb4-4926-a6ed-fee85d3c4370", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3.3333333333333335" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"10.0 / 3.0" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"id": "c7dd9195-45bc-40c5-8667-9cc20678cded", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"10.0" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Convertir a flotante\n", | |
"10.to_f" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"id": "c20dde12-e6d4-4a4f-9e29-f814f9f858c7", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"12" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# convertir a entero\n", | |
"12.5.to_i" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"id": "30e301cf-ab05-495b-b0d0-45883d1ae317", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"10" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"-10.abs" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"id": "d39b094f-3558-4c94-bfa6-5401e3fc3067", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"false" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"3.even?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"id": "76eaa566-07e3-438e-8934-378d62c4764a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3" | |
] | |
}, | |
"execution_count": 15, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"2.next" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "23fbc3b0-e0db-45eb-a3d4-26da116378a9", | |
"metadata": {}, | |
"source": [ | |
"# String" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"id": "8e3e0046-5917-45ba-9fb1-468a2c3d59ab", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Hola mundo\"" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"Hola mundo\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"id": "413c398d-28e6-4bb9-bcf6-7bfcf4df7201", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Hola mundo\"" | |
] | |
}, | |
"execution_count": 17, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"'Hola mundo'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"id": "882c8b41-0f3d-4dbf-8ec2-9316c580a40a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"'Hola' mundo\"" | |
] | |
}, | |
"execution_count": 18, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"'Hola' mundo\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "bcf90e8b-e969-4795-9bbe-85fc066a4dec", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Hola Juan\"" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# se concatena con +\n", | |
"nombre = 'Juan'\n", | |
"\"Hola \" + nombre" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"id": "81e0de9d-52ff-4629-b06b-5dfeef41fd24", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Hola Juan\"" | |
] | |
}, | |
"execution_count": 21, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# interpolaciones\n", | |
"\"Hola #{ nombre }\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"id": "30f5acdb-f81e-4419-a750-cc9f28d6f3ec", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Hola 4\"" | |
] | |
}, | |
"execution_count": 22, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"Hola #{ 2 + 2 }\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"id": "00ab6ae4-0ba3-46a8-9f15-0b48d945a36c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Hola JUAN\"" | |
] | |
}, | |
"execution_count": 23, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"Hola #{ nombre.upcase }\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "17a47e73-619d-4b00-877f-5a158fadc188", | |
"metadata": {}, | |
"source": [ | |
"* IMPORTANTE: las interpolaciones solo funcionan con \", pero no con '" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "02afe8ed-e110-4eb4-9fa4-50c40e1dcc6e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Hola \\#{ nombre.upcase }\"" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"'Hola #{ nombre.upcase }'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"id": "db1a0c10-6fca-41b4-8e97-67666635afec", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"juan\"" | |
] | |
}, | |
"execution_count": 25, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"JUAN\".downcase" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"id": "3427eccf-88bd-41e5-a96f-3e461ac54ca8", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Juan \n", | |
" \n", | |
" \n", | |
" \tSantos\n" | |
] | |
} | |
], | |
"source": [ | |
"puts \"Juan \\n \\n \\n \\tSantos\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"id": "1fe0d40e-695a-4eb7-bd54-ad64d8a5e77f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"2\n", | |
"3.0\n", | |
"11\n" | |
] | |
} | |
], | |
"source": [ | |
"puts \"2\".to_i # convierte string a entero\n", | |
"puts \"3\".to_f # convierte string a flotante\n", | |
"puts 11.to_s # convierte entero a string" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "305f528e-1ed3-4b99-9f46-e1ad0d1714c9", | |
"metadata": {}, | |
"source": [ | |
"# IO" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"id": "221a6402-7598-4ff2-a0c0-935dba734b74", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\" \\\"iopub_port\\\": 63116,\\n\"" | |
] | |
}, | |
"execution_count": 46, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# No funciona dentro del notebook\n", | |
"nombre = gets" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 51, | |
"id": "6f1dd47e-6e65-4f0b-878d-f888e4850ec8", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hola tu nombre \"iopub_port\": 63116, tiene 22\n" | |
] | |
} | |
], | |
"source": [ | |
"nombre = nombre.chomp # Quita salto de línea al final\n", | |
"puts \"Hola tu nombre #{nombre} tiene #{ nombre.length }\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "a3858d5e-a558-423f-8596-b2fef44e9d58", | |
"metadata": {}, | |
"source": [ | |
"# Sintaxis" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 57, | |
"id": "a218e19c-a5c5-42f5-834f-b36d834ecf51", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"4" | |
] | |
}, | |
"execution_count": 57, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"=begin\n", | |
"Esto es un comentario multilinea\n", | |
"a = 1\n", | |
"b=2\n", | |
"c=3\n", | |
"=end\n", | |
"# Normalmente se usa # en lugar de begin, end\n", | |
"d = 4" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "c22ef36b-de92-4c47-b7fc-4e12bab136e4", | |
"metadata": {}, | |
"source": [ | |
"* En ruby los paréntesis son opcionales\n", | |
" * Sin paréntesis cuando pertenece a DSL (domain specific Language)\n", | |
" * Con paréntesis para los demás para pasar parámetros" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "c19847ea-c7ea-462c-ae7e-c2f4c68a126a", | |
"metadata": {}, | |
"source": [ | |
"# Operadores de comparación" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 58, | |
"id": "67629819-a70c-40c2-b8a7-a2f70c2ee1d0", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"false" | |
] | |
}, | |
"execution_count": 58, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"2 > 3" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 62, | |
"id": "22b9cf28-559b-4d09-a851-ea1c25e2a4b9", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"false" | |
] | |
}, | |
"execution_count": 62, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"2 >= 3" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 63, | |
"id": "daa1237e-c552-4858-a9bd-8f56c48148c3", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"false" | |
] | |
}, | |
"execution_count": 63, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"2 == 3" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 77, | |
"id": "9d8c580e-4009-487c-894c-b36d8035b7af", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"true" | |
] | |
}, | |
"execution_count": 77, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"2 != 3" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 69, | |
"id": "d8f81d81-635e-49d6-8980-00ee19736373", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"-1" | |
] | |
}, | |
"execution_count": 69, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# operador de comparación combinado: retorna 10 si son iguales, 1 si el de la izquierda es mayor y -1 si el de la derecha es mayor\n", | |
"1 <=> 2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 70, | |
"id": "0443ca9e-416d-4f6b-a326-38a9553d7c72", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"true" | |
] | |
}, | |
"execution_count": 70, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"2 != 3" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 72, | |
"id": "8760dbe5-bc03-4b18-b123-44f2e7d333f1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"false" | |
] | |
}, | |
"execution_count": 72, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"Juan\" == \"juan\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 76, | |
"id": "7adf393b-3f17-42a4-9931-ea89e800176c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"72640\n", | |
"72660\n", | |
"false\n" | |
] | |
} | |
], | |
"source": [ | |
"puts \"juan\".object_id\n", | |
"puts \"juan\".object_id\n", | |
"puts \"juan\".equal?(\"juan\") # compara el object id" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "e819f91b-31d7-4529-b114-f1a6a965b8ff", | |
"metadata": {}, | |
"source": [ | |
"# Operadores aritméticos" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 85, | |
"id": "5f9e4ef2-b3d1-43f3-b92e-735476f00441", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"15\n", | |
"5\n", | |
"50\n", | |
"2\n", | |
"0\n", | |
"100000\n" | |
] | |
} | |
], | |
"source": [ | |
"x = 10\n", | |
"y = 5\n", | |
"puts x + y\n", | |
"puts x - y\n", | |
"puts x * y\n", | |
"puts x / y\n", | |
"puts x % y\n", | |
"puts x ** y" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "35e490cc-2fb7-433b-bcc4-ef2f32eb43a7", | |
"metadata": {}, | |
"source": [ | |
"# Operadores lógicos" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 86, | |
"id": "854841f3-0305-4cfc-b587-89d3ca66c0f1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"true" | |
] | |
}, | |
"execution_count": 86, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"(10 > 5) && (5 < 8)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 87, | |
"id": "d2949a85-d8b8-428e-8004-713556b7d5fc", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"true" | |
] | |
}, | |
"execution_count": 87, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"(10 > 5) || (5 < 8)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 88, | |
"id": "a216f085-5a5f-4e18-ab42-f3bf06db4365", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"true" | |
] | |
}, | |
"execution_count": 88, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"(10 > 5) || (jdjhhdgf) # SI la primera es cierta, lo segundo no se evalúa" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 91, | |
"id": "b628a5b2-a6c2-48b6-8db7-802e46a7264d", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"false" | |
] | |
}, | |
"execution_count": 91, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"!true" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 92, | |
"id": "6503ee36-b6eb-48a4-ad18-b0432c5efcc6", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"false" | |
] | |
}, | |
"execution_count": 92, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"not true" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "0500c3da-bf58-4145-9fcc-90f7694260dc", | |
"metadata": {}, | |
"source": [ | |
"# Condicionales" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 102, | |
"id": "2786c089-cd8e-4d7d-a67e-dd41fc372ae0", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Numero uno es 1 y número dos es 4\n" | |
] | |
} | |
], | |
"source": [ | |
"numero_1 = 1\n", | |
"numero_2 = 4\n", | |
"puts \"Numero uno es #{numero_1} y número dos es #{numero_2}\"\n", | |
"\n", | |
"if numero_1 > numero_2\n", | |
" puts \"#{numero_1} es mayor que #{numero_2}\"\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 105, | |
"id": "c4183330-4845-4ad6-822c-c8782a724873", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"puts \"#{numero_1} es mayor que #{numero_2}\" if numero_1 > numero_2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 108, | |
"id": "6c25f4d6-0894-4ad6-9ac5-dc92e683f895", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1 es menor que 4\n" | |
] | |
} | |
], | |
"source": [ | |
"if numero_1 > numero_2\n", | |
" puts \"#{numero_1} es mayor que #{numero_2}\"\n", | |
"elsif numero_1 == numero_2\n", | |
" puts \"#{numero_1} es igual a #{numero_2}\"\n", | |
"else\n", | |
" puts \"#{numero_1} es menor que #{numero_2}\"\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 112, | |
"id": "3c38badd-5b76-4cf0-a4d3-5336581168e5", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Lo contrario de if\n" | |
] | |
} | |
], | |
"source": [ | |
"unless numero_1 > numero_2 # unless es lo contrario de if, evalúa si es falso\n", | |
" puts \"Lo contrario de if\"\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "5c341162-3733-429b-b382-7243f970ede9", | |
"metadata": {}, | |
"source": [ | |
"## Operador ternario" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 114, | |
"id": "7ec164d5-7bc7-496b-98c9-55c6de4d799f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Tutor\n" | |
] | |
} | |
], | |
"source": [ | |
"user = \"juan\"\n", | |
"if user == \"juan\"\n", | |
" puts \"Tutor\"\n", | |
"else\n", | |
" puts \"visitante\"\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 115, | |
"id": "8bd2fb10-7866-4fa1-8f72-2be5814e45a5", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Tutor\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"puts (if user == \"juan\" then puts \"Tutor\" else puts \"visitante\" end)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "17e05000-8531-4aba-a2b5-33d0c147eff1", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"puts (if user == \"juan\" then puts \"Tutor\" else puts \"visitante\" end)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 120, | |
"id": "469a1aab-a1ef-496a-8881-17e2b8e62819", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Tutor\n" | |
] | |
} | |
], | |
"source": [ | |
"puts user == \"juan\" ? \"Tutor\" : \"Vistante\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "18c4b9c1-7fb1-4987-8f26-97e1585b5b14", | |
"metadata": {}, | |
"source": [ | |
"# Case" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 125, | |
"id": "677982dc-aa39-44b4-b47b-0ca1e7c7b545", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Normal\n" | |
] | |
} | |
], | |
"source": [ | |
"calificacion = 6\n", | |
"puts case calificacion\n", | |
"when 10, 9\n", | |
" \"muy bien\"\n", | |
"when 8, 7\n", | |
" \"bien\"\n", | |
"when 6\n", | |
" \"Normal\"\n", | |
"else\n", | |
" \"uwu\"\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "b41b3bca-0e51-442e-b7fd-a65000925f87", | |
"metadata": {}, | |
"source": [ | |
"# Arreglos" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"id": "a79c74a9-1600-4032-a7a1-2e3fcdc31797", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, \"juan\", true]" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arreglo = [1, \"juan\", true]\n", | |
"arreglo" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"id": "ccde394c-9486-40d2-818e-cb6bc7977e80", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[nil, nil, nil, nil, nil]" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arreglo_dos = Array.new(5)\n", | |
"arreglo_dos" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"id": "eed193ce-d4e1-4e12-9e2a-273ad0953ab8", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"juan\"" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arreglo[1]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"id": "fb5f0855-d3d5-48fc-8db7-c95b11dd09e5", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"arreglo[5] # retorna nil si la posición no existe dentro del arreglo" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 141, | |
"id": "70dce416-5ef8-41b1-bb8c-5bdb45ece9df", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[\"1\", \"40\", \"'juan\", \"J'\"]" | |
] | |
}, | |
"execution_count": 141, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arreglo = %w[1 40 'juan J'] # si no quieres separar elemento usando comas. OJO: retorna todos los elementos en string" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"id": "21f2963b-0bc5-4e3e-85b6-bd47ba1602db", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, \"juan\", true, \"hola\", 3]" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arreglo << \"hola\" # añade elemento dentro del arreglo en la última posición\n", | |
"arreglo << 3" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 75, | |
"id": "fb726db7-b0ab-4769-94ff-d2d246ca3505", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"2\n", | |
"3\n" | |
] | |
} | |
], | |
"source": [ | |
"# unpack\n", | |
"a, b, c = [1, 2, 3]\n", | |
"puts a, b, c" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "81b78b78-5a57-4a8e-81cf-2103ee7c392a", | |
"metadata": {}, | |
"source": [ | |
"## iteradores de arreglos" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"id": "5cb0f7bb-8384-4e13-9e0c-c090d8cb0ce8", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Ahora calificacion vale: 1\n", | |
"Ahora calificacion vale: 2\n", | |
"Ahora calificacion vale: 4\n", | |
"Ahora calificacion vale: 10\n", | |
"Ahora calificacion vale: 8\n", | |
"Ahora calificacion vale: 7\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2, 4, 10, 8, 7]" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"calificaciones = [1, 2, 4, 10, 8, 7]\n", | |
"calificaciones.each do |calificacion|\n", | |
" puts \"Ahora calificacion vale: #{calificacion}\"\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "784e4163-78f7-4866-bc06-1f739aa84a2f", | |
"metadata": {}, | |
"source": [ | |
"* También lo puedo escribir como:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"id": "1b72a763-5ee5-43e6-80d6-03a506fc6112", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Ahora calificacion vale: 1\n", | |
"Ahora calificacion vale: 2\n", | |
"Ahora calificacion vale: 4\n", | |
"Ahora calificacion vale: 10\n", | |
"Ahora calificacion vale: 8\n", | |
"Ahora calificacion vale: 7\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2, 4, 10, 8, 7]" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"calificaciones.each { |calificacion| puts \"Ahora calificacion vale: #{calificacion}\"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"id": "a3615681-979a-4d98-922f-546b28591634", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Ahora calificacion vale: 1 y está en la posición 0\n", | |
"Ahora calificacion vale: 2 y está en la posición 1\n", | |
"Ahora calificacion vale: 4 y está en la posición 2\n", | |
"Ahora calificacion vale: 10 y está en la posición 3\n", | |
"Ahora calificacion vale: 8 y está en la posición 4\n", | |
"Ahora calificacion vale: 7 y está en la posición 5\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2, 4, 10, 8, 7]" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"calificaciones.each_with_index do |calificacion, posicion|\n", | |
" puts \"Ahora calificacion vale: #{calificacion} y está en la posición #{posicion}\"\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"id": "77e1d00e-3364-4a5d-a246-6be3391b25e8", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"El promedio es 5.333333333333333\n" | |
] | |
} | |
], | |
"source": [ | |
"suma = 0\n", | |
"calificaciones.each do |calificacion|\n", | |
" suma += calificacion\n", | |
"end\n", | |
"puts \"El promedio es #{ suma.to_f / calificaciones.length }\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "502eee4e-e32c-45f6-842c-595c6ae1eb4a", | |
"metadata": { | |
"tags": [] | |
}, | |
"source": [ | |
"## Métodos de arreglos" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "2bd4939e-771e-4882-b146-6e5a7b6b94c4", | |
"metadata": {}, | |
"source": [ | |
"### multiplicación por entero" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 150, | |
"id": "6f531ee5-783c-4243-b48d-81cf01513d1c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2, 4, 10, 8, 7, 1, 2, 4, 10, 8, 7]" | |
] | |
}, | |
"execution_count": 150, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"calificaciones * 2 # replica n veces los elementos del arreglo" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "32f1fd07-9925-4d36-80ef-f7f5bb916f4a", | |
"metadata": {}, | |
"source": [ | |
"### join" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 153, | |
"id": "cf11f05c-0ea6-4a66-9c53-b239d25150ce", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"1,2,4,10,8,7\"" | |
] | |
}, | |
"execution_count": 153, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"calificaciones.join(\",\") # retorna un string concatenando todos los elementos usando un caracter como separador" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "d7ff1dd2-a737-471b-b275-cc00a0931a48", | |
"metadata": {}, | |
"source": [ | |
"### sort" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 154, | |
"id": "97d4be61-9d7d-493c-8156-3db22dbfecf3", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2, 4, 7, 8, 10]" | |
] | |
}, | |
"execution_count": 154, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"calificaciones.sort # ordena de forma ascendente" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "c09b1468-d0c8-4895-afca-8d9363bab976", | |
"metadata": {}, | |
"source": [ | |
"### reverse" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 156, | |
"id": "a2570c1d-8984-48be-9769-043cf819b476", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[7, 8, 10, 4, 2, 1]" | |
] | |
}, | |
"execution_count": 156, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"calificaciones.reverse # Retorna los elementos en el orden inverso" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 159, | |
"id": "9310051b-6ca9-4830-9407-7d56ba52b03f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[10, 8, 7, 4, 2, 1]" | |
] | |
}, | |
"execution_count": 159, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"calificaciones.sort.reverse # ordena de forma descendente" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "f8843a4d-094f-4730-86e3-61818b836560", | |
"metadata": {}, | |
"source": [ | |
"### include?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "848d15f7-3bc8-4bb8-9116-6d2283942034", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"false" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"calificaciones.include?(6) # verifica si elemento está incluído" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "5d1ae840-8ade-4b24-8086-597a738d4134", | |
"metadata": {}, | |
"source": [ | |
"### first & last" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "0ca43d2d-08b2-4aa1-995d-5d196aebd298", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"calificaciones.first # primer elemento" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "9429328d-ba0e-4b7e-91e0-2eb3345dfa42", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"7" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"calificaciones.last # ultimo elemento" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "7e046622-07c7-4dd3-80c2-b7126acd03a5", | |
"metadata": {}, | |
"source": [ | |
"### index" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 66, | |
"id": "52dec17b-6122-424a-a888-4c4ed5cce6e9", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3" | |
] | |
}, | |
"execution_count": 66, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"calificaciones.index(10) # retorna el índice del elemento especificado" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "559514f7-7597-42ee-b20b-6d83cf5c79bb", | |
"metadata": {}, | |
"source": [ | |
"### uniq" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "fb3db2a6-8710-4132-8ac9-8d4eb40fbd0b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2, 4, 10, 8, 7]" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"calificaciones.uniq # solo elementos unicos" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "cea66c99-1ed4-4e4c-9f60-57b2b706b27b", | |
"metadata": {}, | |
"source": [ | |
"### sample" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"id": "ef895b77-44de-4657-80b4-5fb9910d4ff2", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1" | |
] | |
}, | |
"execution_count": 37, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"calificaciones.sample # retorna un elemento aleatorio" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "b0d27cf5-404e-4081-baac-10637a890713", | |
"metadata": {}, | |
"source": [ | |
"### min & max " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 88, | |
"id": "3f546788-399c-4e6d-b851-02eba37ce7b0", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"10" | |
] | |
}, | |
"execution_count": 88, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"calificaciones.max # retorna el valor máximo" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 65, | |
"id": "0afe94c6-fab7-4e72-a7b9-bc86bae11c55", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1" | |
] | |
}, | |
"execution_count": 65, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"calificaciones.min # retorna el valor mínimo" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "7d9ca7ae-4d7a-4b6b-a886-71f375fef2fd", | |
"metadata": {}, | |
"source": [ | |
"### map (collect)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"id": "b396ea50-4ef0-4a4c-9669-08af2d79eccb", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 4, 16, 100, 64, 49]" | |
] | |
}, | |
"execution_count": 45, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"calificaciones.map {|num| num ** 2} # aplica una función a cada elemento del array" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"id": "96122efd-c7b9-4367-8092-73025a83f6e4", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 4, 16, 100, 64, 49]" | |
] | |
}, | |
"execution_count": 49, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"calificaciones.collect {|num| num ** 2} # collect funciona igual que map" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "4692124f-a3f1-4677-9362-c247accac257", | |
"metadata": {}, | |
"source": [ | |
"### select, reject, & partition" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "1e9e30d4-ca3c-43a2-a603-de02aaff46b2", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"calificaciones.select {|cal| cal >= 5} # retorna los elementos que cumplen con cierta condición" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 70, | |
"id": "e617b4dc-6e50-4bbd-adea-9bc2053eeef1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2, 4]" | |
] | |
}, | |
"execution_count": 70, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# retorna los elementos que NO cumplen con cierta condición, lo opuesto de select\n", | |
"calificaciones.reject {|cal| cal >= 5}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 72, | |
"id": "8e4d3290-e190-4130-ace9-a2a994fd47ae", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[[10, 8, 7], [1, 2, 4]]" | |
] | |
}, | |
"execution_count": 72, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# retorna un arreglo con 2 arreglos: los elementos que cumplen la condición y oro arreglo con los que no\n", | |
"calificaciones.partition {|cal| cal >= 5} " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "5d8d2462-5284-4873-b2f6-e72a34c57a99", | |
"metadata": {}, | |
"source": [ | |
"### find" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 76, | |
"id": "e2b19c4b-9aab-469d-b3e4-d5b0935fde96", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"10" | |
] | |
}, | |
"execution_count": 76, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# selecciona el primer elemento que cumple con cierta condición\n", | |
"calificaciones.find {|cal| cal >= 5} " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "0155f1ce-9bf6-4361-91f6-991beb8971e1", | |
"metadata": {}, | |
"source": [ | |
"### any & all" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 79, | |
"id": "d2db402c-12c4-4568-83f4-14ae3cfae5d8", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"true" | |
] | |
}, | |
"execution_count": 79, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# verifica si alguno de los elementos cumple con cierta condición\n", | |
"calificaciones.any? {|cal| cal >= 5} " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 78, | |
"id": "80108f9f-f777-43ab-acc7-fffd70b200da", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"false" | |
] | |
}, | |
"execution_count": 78, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# verifica si todos los elementos cumple con cierta condición\n", | |
"calificaciones.all? {|cal| cal >= 5} " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "dc716cd7-8888-41d4-bab6-d7618606fcb9", | |
"metadata": {}, | |
"source": [ | |
"### compact" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 81, | |
"id": "d0be74f8-9963-493b-9c8f-6c0dcf268531", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2, 4, 6]" | |
] | |
}, | |
"execution_count": 81, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# retorna un array removiendo todos los nil\n", | |
"arr_1 = [1, 2, nil, 4, 6, nil]\n", | |
"arr_1.compact" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "9d35e65b-5f60-46cf-9643-14897fb797e0", | |
"metadata": {}, | |
"source": [ | |
"### reduce (inject)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 89, | |
"id": "f1e4fc18-d5b3-4076-b0cf-a46f4e7a937f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"4480" | |
] | |
}, | |
"execution_count": 89, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# determina como operar dos elementos consecutivos del array acumulativamente\n", | |
"calificaciones.reduce(1) {|prev, current| prev * current}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "d91fb02e-02bd-440e-a05f-7277ff7d8ea3", | |
"metadata": {}, | |
"source": [ | |
"### concat\n", | |
"* concatena 2 arrays, modifica el objeto original" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 91, | |
"id": "48415d3c-cb12-4b58-896a-320b563e28e0", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[4, 5, 6]" | |
] | |
}, | |
"execution_count": 91, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arr_1 = [1, 2, 3]\n", | |
"arr_2 = [4, 5, 6]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 92, | |
"id": "d9bc3f5a-3296-4851-8e07-32e4ac5f1d70", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2, 3, 4, 5, 6]" | |
] | |
}, | |
"execution_count": 92, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arr_1.concat(arr_2)\n", | |
"arr_1" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "0333f5d9-2b29-4916-859a-a65187bbc65b", | |
"metadata": {}, | |
"source": [ | |
"### zip" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 95, | |
"id": "56e450f8-aa69-4026-885e-fec71f089f29", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[[1, 4], [2, 5], [3, 6]]" | |
] | |
}, | |
"execution_count": 95, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# conforma pares de arrays basados en la posiciónd e cada array individual\n", | |
"arr_1 = [1, 2, 3]\n", | |
"arr_2 = [4, 5, 6]\n", | |
"arr_1.zip(arr_2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "a585183c-f8e1-4b0a-a597-b5fcb17a4e61", | |
"metadata": {}, | |
"source": [ | |
"### union" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 99, | |
"id": "3e7c784f-14a0-4033-9e1d-bf70fb21d559", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2, 3, 5]" | |
] | |
}, | |
"execution_count": 99, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# concatena 2 arrays y conserva solo los elementos únicos\n", | |
"arr_1 = [1, 2, 3]\n", | |
"arr_2 = [1, 5, 2]\n", | |
"arr_1.union(arr_2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 105, | |
"id": "573165f1-9751-4eb9-9d55-4614394d7bf8", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2, 3, 5]" | |
] | |
}, | |
"execution_count": 105, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# también se puede usar símbolo |\n", | |
"arr_1 | arr_2" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "02ebe362-1364-49a0-b4ad-dd637e80d3f5", | |
"metadata": {}, | |
"source": [ | |
"### difference" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 102, | |
"id": "95fe92ff-81b3-4fa0-ab85-dbb8283e196a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[3]" | |
] | |
}, | |
"execution_count": 102, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# elimina de un array los elementos que están presente en otros\n", | |
"arr_1 = [1, 2, 3]\n", | |
"arr_2 = [1, 5, 2]\n", | |
"arr_1.difference(arr_2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 101, | |
"id": "d88174e8-d22a-469d-906a-ab6c1b722ae3", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[3]" | |
] | |
}, | |
"execution_count": 101, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# también se puede usar símbolo -\n", | |
"arr_1 - arr_2" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "2ea99bcf-e091-4ae3-988a-e298ed4a40c7", | |
"metadata": {}, | |
"source": [ | |
"### intersection" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 103, | |
"id": "45877f56-325c-4bd4-88a2-fe07e9de0d66", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2]" | |
] | |
}, | |
"execution_count": 103, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# retorna los elementos en común que tienen 2 arrays\n", | |
"arr_1 = [1, 2, 3]\n", | |
"arr_2 = [1, 5, 2]\n", | |
"arr_1.intersection(arr_2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 104, | |
"id": "02b186b2-2299-4bb4-a3bc-e00f8638fff6", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2]" | |
] | |
}, | |
"execution_count": 104, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# también se puede ejecutar con símbolo &\n", | |
"arr_1 & arr_2" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "b2ded04c-1ab9-43d6-b0ca-9b964c5283eb", | |
"metadata": {}, | |
"source": [ | |
"## Rangos" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"id": "9a1cde10-2e80-4ae2-8a3b-0d66381a455c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n", | |
"5\n", | |
"6\n", | |
"7\n", | |
"8\n", | |
"9\n", | |
"10\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"1..10" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"numeros = (1..10) # los rangos no son arreglos!\n", | |
"numeros.each do |numero|\n", | |
" puts numero\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"id": "43388403-a020-44e3-a5ad-f269c1a5d1de", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"3\n", | |
"5\n", | |
"7\n", | |
"9\n", | |
"11\n", | |
"13\n", | |
"15\n", | |
"17\n", | |
"19\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"((1..20).step(2))" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"(1..20).step(2).each do |numero| # cada 2 números\n", | |
" puts numero\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"id": "b3ec97b5-5713-40cd-8ca4-4bdc40a9ce54", | |
"metadata": { | |
"scrolled": true, | |
"tags": [] | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z," | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"\"a\"..\"z\"" | |
] | |
}, | |
"execution_count": 24, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"('a'..'z').each do |numero| # también funciona con strings\n", | |
" print numero + ','\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"id": "773be8b3-5c94-4519-b0fa-2703a7744dd0", | |
"metadata": { | |
"scrolled": true, | |
"tags": [] | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"20\n", | |
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][\"ma\", \"mb\", \"mc\", \"md\"]" | |
] | |
} | |
], | |
"source": [ | |
"puts (1..20).min # mínimo\n", | |
"puts (1..20).max # Máximo\n", | |
"print (1..20).to_a # Convierte en arreglo\n", | |
"print ('ma'..'md').to_a" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "44ac6f1f-f25b-44dd-a07c-4045f99ff362", | |
"metadata": {}, | |
"source": [ | |
"# Otros iteradores" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "014b6b16-f48d-44c6-8225-aad8a04dd621", | |
"metadata": {}, | |
"source": [ | |
"## While y until" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"id": "5de8576e-a44f-4832-aa65-d41cbe791fd8", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0\n", | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n", | |
"5\n", | |
"6\n", | |
"7\n", | |
"8\n", | |
"9\n" | |
] | |
} | |
], | |
"source": [ | |
"i = 0\n", | |
"while i < 10\n", | |
" puts i\n", | |
" i += 1\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"id": "67aaa2e3-676d-4609-abc7-7073e85ccae9", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0\n", | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n", | |
"5\n", | |
"6\n", | |
"7\n", | |
"8\n", | |
"9\n" | |
] | |
} | |
], | |
"source": [ | |
"# until es lo contrario a while\n", | |
"i = 0\n", | |
"until i == 10\n", | |
" puts i\n", | |
" i += 1\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"id": "064e65bf-0b67-4f99-bb79-8dcf9a83eeac", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"-3\n", | |
"-2\n", | |
"-1\n" | |
] | |
} | |
], | |
"source": [ | |
"# do while: Garantiza que el bloque de código se correrá al menos una vez\n", | |
"numero = -3\n", | |
"begin\n", | |
" puts numero\n", | |
" numero += 1\n", | |
"end while numero < 0" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "893bb8e1-4f6e-44b7-bd95-6aaf667809fb", | |
"metadata": {}, | |
"source": [ | |
"## `break` keyword\n", | |
" * Forza el loop a detenerse, puede ser un each loop o while" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 55, | |
"id": "6b2f956c-a957-4912-886c-4996fcac0f59", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"4\n", | |
"36\n", | |
"Aquí me detengo\n" | |
] | |
} | |
], | |
"source": [ | |
"cals = [1, 2, 6, \"jj\", 8, 90]\n", | |
"cals.each do |num|\n", | |
" if num.is_a?(Fixnum)\n", | |
" puts \"#{num ** 2}\"\n", | |
" else\n", | |
" puts \"Aquí me detengo\"\n", | |
" break\n", | |
" end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 53, | |
"id": "94466edf-c401-4139-9411-48bfdbdbd61f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"No encontramos a Javier!\n", | |
"No encontramos a Javier!\n", | |
"encontramos a Javier!\n" | |
] | |
} | |
], | |
"source": [ | |
"nombres = [\"Juan\", \"Juan\", \"Javier\", \"Juan\", \"Juan\"]\n", | |
"i = 0\n", | |
"while i < nombres.length\n", | |
" current = nombres[i]\n", | |
" if current == \"Javier\"\n", | |
" puts \"encontramos a #{current}!\"\n", | |
" break\n", | |
" else\n", | |
" puts \"No encontramos a Javier!\"\n", | |
" end\n", | |
" i += 1\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "2cc5fca5-6cd1-46df-b456-f6b59212bfb2", | |
"metadata": {}, | |
"source": [ | |
"## `next` keyword\n", | |
"* al invocarse, el loop detiene la ejecución de la iteración del elemento actual e inicia con el elemento siguiente" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 56, | |
"id": "26103fe1-b5a8-4eae-9cc9-ff0a6fd2d45c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"4\n", | |
"36\n", | |
"64\n", | |
"8100\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2, 6, \"jj\", 8, 90]" | |
] | |
}, | |
"execution_count": 56, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"cals = [1, 2, 6, \"jj\", 8, 90]\n", | |
"cals.each do |num|\n", | |
" unless num.is_a?(Fixnum)\n", | |
" next\n", | |
" else\n", | |
" puts \"#{num ** 2}\"\n", | |
" end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "325b343c-f045-4412-b860-61fb7bd57dba", | |
"metadata": {}, | |
"source": [ | |
"## Times" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 47, | |
"id": "bbccc602-c6b7-4cdb-87e6-146a9a20eb92", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n", | |
"5\n", | |
"6\n", | |
"7\n", | |
"8\n", | |
"9\n", | |
"10\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"10" | |
] | |
}, | |
"execution_count": 47, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# el ciclo se definirá n veces. Si es un número, la variable del ciclo irá aumentando en 1\n", | |
"10.times do |i|\n", | |
" puts i + 1\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"id": "e99a3eb0-2610-469c-92d2-2a5019562081", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"imprime esto\n", | |
"imprime esto\n", | |
"imprime esto\n", | |
"imprime esto\n", | |
"imprime esto\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"5" | |
] | |
}, | |
"execution_count": 48, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"5.times do\n", | |
" puts \"imprime esto\"\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "953934b0-1619-401c-b222-b126e5a44670", | |
"metadata": {}, | |
"source": [ | |
"## Upto y downto" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"id": "d0c47b5e-ec7d-49d2-bba3-964e3ee52bb1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n", | |
"5\n", | |
"6\n", | |
"7\n", | |
"8\n", | |
"9\n", | |
"10\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"1" | |
] | |
}, | |
"execution_count": 49, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"1.upto(10) do |i|\n", | |
" puts i\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"id": "a47380ac-067b-4bcb-b5b2-b11aebfc2613", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"10\n", | |
"9\n", | |
"8\n", | |
"7\n", | |
"6\n", | |
"5\n", | |
"4\n", | |
"3\n", | |
"2\n", | |
"1\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"10" | |
] | |
}, | |
"execution_count": 50, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"10.downto(1) do |i|\n", | |
" puts i\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "1aa47264-86c6-452b-9c77-92bf1a8c9aca", | |
"metadata": {}, | |
"source": [ | |
"# Métodos de string" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"id": "5f738c3c-aab7-419f-83d6-eaef44f5862a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[:unicode_normalized?, :encode!, :unicode_normalize, :ascii_only?, :unicode_normalize!, :to_r, :to_c, :encode, :pretty_print, :include?, :%, :*, :+, :unpack, :unpack1, :count, :partition, :+@, :-@, :<=>, :<<, :sum, :==, :===, :next, :=~, :[], :[]=, :empty?, :casecmp, :eql?, :insert, :casecmp?, :match?, :bytesize, :match, :next!, :succ!, :index, :upto, :replace, :rindex, :chr, :clear, :byteslice, :getbyte, :setbyte, :freeze, :scrub, :scrub!, :dump, :inspect, :intern, :upcase, :downcase, :capitalize, :swapcase, :upcase!, :undump, :length, :size, :downcase!, :succ, :swapcase!, :hex, :capitalize!, :split, :chars, :oct, :grapheme_clusters, :concat, :codepoints, :lines, :bytes, :to_str, :end_with?, :start_with?, :reverse, :reverse!, :sub, :to_s, :to_i, :to_f, :rjust, :center, :prepend, :crypt, :ord, :chomp, :strip, :to_sym, :ljust, :delete_prefix, :delete_suffix, :lstrip, :gsub, :scan, :chomp!, :sub!, :gsub!, :rstrip, :delete_prefix!, :chop, :lstrip!, :rstrip!, :chop!, :delete_suffix!, :strip!, :tr_s, :delete, :squeeze, :tr!, :tr, :delete!, :squeeze!, :each_line, :each_byte, :tr_s!, :each_codepoint, :each_grapheme_cluster, :slice, :slice!, :each_char, :encoding, :force_encoding, :b, :valid_encoding?, :rpartition, :hash, :to_json, :to_json_raw, :to_json_raw_object, :between?, :clamp, :<=, :>=, :<, :>, :pretty_print_instance_variables, :pretty_print_cycle, :pretty_print_inspect, :singleton_class, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :display, :class, :frozen?, :tap, :then, :yield_self, :public_send, :pretty_inspect, :extend, :clone, :method, :public_method, :singleton_method, :define_singleton_method, :!~, :nil?, :respond_to?, :object_id, :send, :to_enum, :enum_for, :__send__, :!, :instance_eval, :instance_exec, :!=, :equal?, :__id__]" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"\".methods" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "bcd00930-d1c3-4512-b2cd-74477c4e125f", | |
"metadata": {}, | |
"source": [ | |
"## iterar sobre string" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"id": "c897f425-35d0-4005-9495-6edc9cceca4c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"caracter: H\n", | |
"caracter: o\n", | |
"caracter: l\n", | |
"caracter: a\n", | |
"caracter: \n", | |
"caracter: m\n", | |
"caracter: u\n", | |
"caracter: n\n", | |
"caracter: d\n", | |
"caracter: o\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Hola mundo\"" | |
] | |
}, | |
"execution_count": 20, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# iterar por cada caracter\n", | |
"\"Hola mundo\".each_char {|char| puts \"caracter: #{char}\"}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "27df3f6d-062e-440b-be02-7268cd989865", | |
"metadata": {}, | |
"source": [ | |
"## split" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"id": "f9b1568a-fd68-433c-8547-bb132ae1cb64", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[\"Hola,\", \"mi\", \"nombre\", \"es\", \"Juan.\", \"Aquí\", \"hay\", \"más\", \"espacios\"]" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sentence = \"Hola, mi nombre es Juan. Aquí hay más espacios\"\n", | |
"sentence.split # retorna un array" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"id": "fcad5649-a147-41d6-bb2b-fe246f3c178f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[\"Hola, mi nombre es Juan\", \" Aquí hay más espacios\"]" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sentence.split(\".\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"id": "41d7c92b-0724-43a6-ab06-372bcafbfeba", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hola, tienen 5 caracteres\n", | |
"mi tienen 2 caracteres\n", | |
"nombre tienen 6 caracteres\n", | |
"es tienen 2 caracteres\n", | |
"Juan. tienen 5 caracteres\n", | |
"Aquí tienen 4 caracteres\n", | |
"hay tienen 3 caracteres\n", | |
"más tienen 3 caracteres\n", | |
"espacios tienen 8 caracteres\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[\"Hola,\", \"mi\", \"nombre\", \"es\", \"Juan.\", \"Aquí\", \"hay\", \"más\", \"espacios\"]" | |
] | |
}, | |
"execution_count": 22, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"palabras = sentence.split\n", | |
"palabras.each { |pal| puts \"#{pal} tienen #{pal.length} caracteres\"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"id": "71b9c763-b6bb-4443-b4d4-625394f84b07", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[\"H\", \"o\", \"l\", \"a\", \" \", \"m\", \"u\", \"n\", \"d\", \"o\"]" | |
] | |
}, | |
"execution_count": 21, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# chars retorna un arreglo con cada caracter\n", | |
"\"Hola mundo\".chars" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "914de310-0276-4cd5-9cbb-53062a6008a8", | |
"metadata": {}, | |
"source": [ | |
"## join" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"id": "2c7e30c2-f0fa-4e91-97a2-3a9fcb1b01c1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Hola,<->mi<->nombre<->es<->Juan.<->Aquí<->hay<->más<->espacios\"" | |
] | |
}, | |
"execution_count": 25, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"palabras.join(\"<->\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "a1c8cf92-4c3e-420c-be0f-e834c76821b5", | |
"metadata": {}, | |
"source": [ | |
"## count" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"id": "7dd09537-a81a-4ea5-99a3-a7f642a944bc", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2" | |
] | |
}, | |
"execution_count": 29, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"Hola mundo\".count(\"o\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "277f7cd7-11d1-49b5-9652-6873040642ec", | |
"metadata": {}, | |
"source": [ | |
"* si colocas más de un caracter, busca cada uno individualmente y suma el total, no es la ocurrencia de ambos al tiempo" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"id": "5885587c-c52a-4ed1-b038-c4c589fb0b87", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3" | |
] | |
}, | |
"execution_count": 32, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"Hola mundo\".count(\"Ho\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "dcc0b4c8-95b4-4a5e-b981-72e1e4f09612", | |
"metadata": {}, | |
"source": [ | |
"## index y rindex\n", | |
"* retorna el índice de la primera o última ocurrencia en la que ocurre un caracter" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"id": "e8d26da9-08c4-482a-89bb-ea852b1c9c31", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1" | |
] | |
}, | |
"execution_count": 34, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"Hola mundo\".index(\"o\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"id": "d97c4ed2-1985-40c0-a5b2-5dcaf3e1cd40", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"9" | |
] | |
}, | |
"execution_count": 35, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"Hola mundo\".rindex(\"o\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "2959b426-ed12-42ae-972e-83b2b5fc2b1f", | |
"metadata": {}, | |
"source": [ | |
"* Si pones un valor, busca a partir de ese caracter y no tiene en cuenta antes" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"id": "e0a94bd2-98da-4408-9105-7c26b1cc73cb", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"9" | |
] | |
}, | |
"execution_count": 36, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"Hola mundo\".index(\"o\", 4)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "53b3b8b9-656c-4c44-a07d-dc909fd0fa12", | |
"metadata": {}, | |
"source": [ | |
"## squeeze\n", | |
"* Remueve caracteres duplicados consecutivos" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"id": "9b3d0490-b2d8-4d58-b9f0-1782e30b802c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Hola mundo \"" | |
] | |
}, | |
"execution_count": 38, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"Hooooolaaaaa mundoooo \".squeeze" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"id": "a1400493-e971-4d1b-8e53-06ca334248d7", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Hola mundo \"" | |
] | |
}, | |
"execution_count": 40, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Puedo especificar uno o varios caracteres\n", | |
"\"Hooooolaaaaa mundoooo \".squeeze(\"ao\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "103c16ec-ae97-4a5d-b058-77131bf27a0b", | |
"metadata": {}, | |
"source": [ | |
"# Matrices" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 59, | |
"id": "c12feb36-4419-44a5-a615-4ab134176c3b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$$\\left(\\begin{array}{ccc}\n", | |
" 1&8&2\\\\\n", | |
" 3&9&4\\\\\n", | |
" 7&4&6\\\\\n", | |
"\\end{array}\\right)$$" | |
], | |
"text/plain": [ | |
"Matrix[[1, 8, 2], [3, 9, 4], [7, 4, 6]]" | |
] | |
}, | |
"execution_count": 59, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Un arreglo tiene 1 dimensión. \n", | |
"# La matriz tiene más dimensiones, todos con los mismos elementos.\n", | |
"# todos los elementos deben ser numeros\n", | |
"require 'matrix' # importamos el módulo\n", | |
"m = Matrix[[1, 8, 2], [3, 9, 4], [7, 4, 6]]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 60, | |
"id": "5ba6e79b-0d19-48a2-a434-8bd770b7a97a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"8\n", | |
"2\n", | |
"3\n", | |
"9\n", | |
"4\n", | |
"7\n", | |
"4\n", | |
"6\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/latex": [ | |
"$$\\left(\\begin{array}{ccc}\n", | |
" 1&8&2\\\\\n", | |
" 3&9&4\\\\\n", | |
" 7&4&6\\\\\n", | |
"\\end{array}\\right)$$" | |
], | |
"text/plain": [ | |
"Matrix[[1, 8, 2], [3, 9, 4], [7, 4, 6]]" | |
] | |
}, | |
"execution_count": 60, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"m.each do |i|\n", | |
" puts i\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 61, | |
"id": "bfe3e394-2000-4284-af69-d5033db66782", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"9\n", | |
"6\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/latex": [ | |
"$$\\left(\\begin{array}{ccc}\n", | |
" 1&8&2\\\\\n", | |
" 3&9&4\\\\\n", | |
" 7&4&6\\\\\n", | |
"\\end{array}\\right)$$" | |
], | |
"text/plain": [ | |
"Matrix[[1, 8, 2], [3, 9, 4], [7, 4, 6]]" | |
] | |
}, | |
"execution_count": 61, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"m.each(:diagonal) do |i| # imprime la diagonal\n", | |
" puts i\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 62, | |
"id": "fdc5166f-282b-402c-b613-a0ebb508aa51", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"3\n", | |
"7\n", | |
"4\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/latex": [ | |
"$$\\left(\\begin{array}{ccc}\n", | |
" 1&8&2\\\\\n", | |
" 3&9&4\\\\\n", | |
" 7&4&6\\\\\n", | |
"\\end{array}\\right)$$" | |
], | |
"text/plain": [ | |
"Matrix[[1, 8, 2], [3, 9, 4], [7, 4, 6]]" | |
] | |
}, | |
"execution_count": 62, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"m.each(:strict_lower) do |i| # imprime elementos debajo de la diagonal\n", | |
" puts i\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 64, | |
"id": "a0de256d-8519-4fc3-9cc5-ad95f2444a74", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"false" | |
] | |
}, | |
"execution_count": 64, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"m.diagonal? # verifica si todos los elementos fuera de la diagonal son ceros" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "e2a35c30-d622-4065-8497-a69f158f9fc0", | |
"metadata": {}, | |
"source": [ | |
"# Hashes (diccionarios)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 106, | |
"id": "6a23b21a-8a9f-4b70-afc9-7197449cdc5c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{\"nombre\"=>\"Juan\", \"edad\"=>20, 20=>\"veinte\", []=>\"Arreglo\"}" | |
] | |
}, | |
"execution_count": 106, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"h = {\"nombre\" => \"Juan\", \"edad\" => 20, 20 => \"veinte\", [] => \"Arreglo\"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 70, | |
"id": "0c73685c-393f-4c90-b780-1d39a746a539", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"20\n", | |
"Arreglo\n" | |
] | |
} | |
], | |
"source": [ | |
"puts h['edad']\n", | |
"puts h[[]]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 72, | |
"id": "c6115dae-d014-458c-b4bc-d986c3893a11", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{\"nombre\"=>\"Juan\", \"edad\"=>20, 20=>\"veinte\", []=>\"Arreglo\", \"cursos\"=>5}" | |
] | |
}, | |
"execution_count": 72, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# nueva entrada\n", | |
"h[\"cursos\"] = 5\n", | |
"h" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 74, | |
"id": "4f7e4b58-70c9-4fe1-b1cf-5c0736f931be", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# si la llave no existe, retorna nil\n", | |
"h[1]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 75, | |
"id": "9cc7fc8c-477f-45ef-b5b6-c7a2d9ce401f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\":)\"" | |
] | |
}, | |
"execution_count": 75, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# definimos un default para valores que no existen\n", | |
"h.default = \":)\"\n", | |
"h[1]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 82, | |
"id": "12c9860c-27f3-423a-baf4-56add973875b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"jj\n" | |
] | |
} | |
], | |
"source": [ | |
"# usando símbolos (todavía no hemos hablado de símbolos)\n", | |
"h2 = {nombre: \"jj\", edad: 30}\n", | |
"puts h2[:nombre]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 83, | |
"id": "9fcbbb4c-77cd-4067-9d2a-9ff89a835e6f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"nombre: Juan\n", | |
"edad: 20\n", | |
"20: veinte\n", | |
"[]: Arreglo\n", | |
"cursos: 5\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"{\"nombre\"=>\"Juan\", \"edad\"=>20, 20=>\"veinte\", []=>\"Arreglo\", \"cursos\"=>5}" | |
] | |
}, | |
"execution_count": 83, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"h.each do |clave, valor|\n", | |
" puts \"#{clave}: #{valor}\"\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 107, | |
"id": "f8ce35cf-969e-48cf-b66a-46ca55768425", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"nombre\n", | |
"edad\n", | |
"20\n", | |
"[]\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"{\"nombre\"=>\"Juan\", \"edad\"=>20, 20=>\"veinte\", []=>\"Arreglo\"}" | |
] | |
}, | |
"execution_count": 107, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# itera solo sobre las keys\n", | |
"h.each_key do |clave|\n", | |
" puts \"#{clave}\"\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 109, | |
"id": "59aff098-5fa3-4ed0-821e-f06b3e382148", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Juan\n", | |
"20\n", | |
"veinte\n", | |
"Arreglo\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"{\"nombre\"=>\"Juan\", \"edad\"=>20, 20=>\"veinte\", []=>\"Arreglo\"}" | |
] | |
}, | |
"execution_count": 109, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# itera solo sobre los valores\n", | |
"h.each_value do |value|\n", | |
" puts \"#{value}\"\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "abf416d0-5759-4715-9624-34f5a6e43497", | |
"metadata": {}, | |
"source": [ | |
"## Métodos de hashes" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 86, | |
"id": "e1f3d58e-eeb9-44e0-9d0b-cfdb04078758", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"5" | |
] | |
}, | |
"execution_count": 86, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"h.length # numero de elementos" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 147, | |
"id": "13a6327b-fb19-4390-8913-2652364d731e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"true" | |
] | |
}, | |
"execution_count": 147, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"h.key?(\"edad\") # verifica si existe la llave edad" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 152, | |
"id": "0f49d641-c2f4-4f61-8ace-2f70e9d9d9ba", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"false" | |
] | |
}, | |
"execution_count": 152, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"h.key?(\"ed\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "400729d3-0224-4924-a3f1-36cf827ad9e4", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 91, | |
"id": "ac73e7ce-f261-4206-8609-7e7ca9796148", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[\"nombre\", \"edad\", 20, [], \"cursos\"]" | |
] | |
}, | |
"execution_count": 91, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"h.keys # retorna las keys" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 92, | |
"id": "94458cc6-4dc6-468f-b582-bfe7a3eef447", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[\"Juan\", 20, \"veinte\", \"Arreglo\", 5]" | |
] | |
}, | |
"execution_count": 92, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"h.values # retorna los values" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 94, | |
"id": "f1189203-fbac-473b-a973-84b28290cfda", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"h.delete(\"cursos\") # elimina elemento con key cursos" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 96, | |
"id": "163b4f1b-5514-4a60-a844-154ee9a5eb50", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"false" | |
] | |
}, | |
"execution_count": 96, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"h.empty? # verfica si está vacío" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 98, | |
"id": "01da2259-eb7f-4114-9e47-7cce92d8b312", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"edad\"" | |
] | |
}, | |
"execution_count": 98, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"h.key(20) # retorna la llave de un valor que está dentro del hash" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 153, | |
"id": "b06893b5-97e9-4dff-bff5-93998594bbff", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"true" | |
] | |
}, | |
"execution_count": 153, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"h.value?(\"veinte\") # verifica si tiene el valor" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 101, | |
"id": "37587f8d-69cc-48bd-a1e8-7e22c4f65c35", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{\"Juan\"=>\"nombre\", 20=>\"edad\", \"veinte\"=>20, \"Arreglo\"=>[], 5=>\"cursos\"}" | |
] | |
}, | |
"execution_count": 101, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"h.invert # convierte llaves en valores y los valores en llaves" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 154, | |
"id": "1cbf0824-cc8e-4ba0-9b41-742558e0eb77", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{\"edad\"=>20}" | |
] | |
}, | |
"execution_count": 154, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# retorna solo los elementos que cumple con cierta condición\n", | |
"h.select {|key, value| value.is_a?(Fixnum)}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 157, | |
"id": "ca77fd28-8652-4945-84f3-070509cfa996", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{\"nombre\"=>\"Juan\", \"edad\"=>20, []=>\"Arreglo\"}" | |
] | |
}, | |
"execution_count": 157, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# retorna solo los elementos que NO cumplen con cierta condición\n", | |
"h.reject {|key, value| key.is_a?(Fixnum)}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 104, | |
"id": "1033cce0-c861-473d-bc53-6ee4cee0bc31", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{\"nombre\"=>\"Juan\", \"edad\"=>20, 20=>\"veinte\", []=>\"Arreglo\", \"cursos\"=>5, :nombre=>\"jj\", :edad=>30}" | |
] | |
}, | |
"execution_count": 104, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"h.merge(h2) # combina dos hashes" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "1a7ea015-38de-49e5-ab19-89a836ee5ebc", | |
"metadata": {}, | |
"source": [ | |
"### sort" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 137, | |
"id": "921a3762-198b-4b62-8c43-d37f755ef973", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[[:primero, 30], [:segundo, 10]]" | |
] | |
}, | |
"execution_count": 137, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# ordena segun key\n", | |
"h3 = {primero: 30, segundo: 10}\n", | |
"h3.sort" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 128, | |
"id": "403e6703-1003-4d5d-b215-de1186184ecc", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[[:segundo, 10], [:primero, 30]]" | |
] | |
}, | |
"execution_count": 128, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# orden inverso\n", | |
"h3.sort.reverse" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 143, | |
"id": "fe5aec56-bac5-47e4-bd90-4347cb83e8b3", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[[:segundo, 10], [:primero, 30]]" | |
] | |
}, | |
"execution_count": 143, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# ordena por valor, type es el valor\n", | |
"h3.sort_by {|key, value| value}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "0455b794-271d-48e7-82f5-b20cc4f6f2cb", | |
"metadata": {}, | |
"source": [ | |
"### hash to array & viceversa" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 113, | |
"id": "0d4662cb-b3f3-48cd-8007-5e518d365be4", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[[\"nombre\", \"Juan\"], [\"edad\", 20], [20, \"veinte\"], [[], \"Arreglo\"]]" | |
] | |
}, | |
"execution_count": 113, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# hash to arry\n", | |
"array_hash = h.to_a" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 115, | |
"id": "b77e6519-337c-4cd4-b138-ae8c47fbb36a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{\"nombre\"=>\"Juan\", \"edad\"=>20, 20=>\"veinte\", []=>\"Arreglo\"}" | |
] | |
}, | |
"execution_count": 115, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# array to hash\n", | |
"array_hash.to_h" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "725a9408-7351-4ad7-84aa-351bd4720a67", | |
"metadata": {}, | |
"source": [ | |
"# Símbolos\n", | |
"\n", | |
"* Es una cadena inmutable\n", | |
"* Su beneficio es mejor rendimiento\n", | |
"* Cuándo usarlos?:\n", | |
" * Cuando no necesita modificar el str\n", | |
" * Cuando no necesito usar operaciones de str\n", | |
" * Son muy útiles como nombres, por ejemplo en los hashes" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 105, | |
"id": "0afeef5f-5bc8-4cf6-8806-f9efe5ee3911", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":Juan" | |
] | |
}, | |
"execution_count": 105, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"cadena1 = \"Juan\"\n", | |
"cadena2 = \"Juan\"\n", | |
"simbolo1 = :Juan\n", | |
"simbolo2 = :Juan" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 107, | |
"id": "53a54770-dc11-45fb-be9a-4ae7dc5e0423", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"49420\n", | |
"49440\n", | |
"3682148\n", | |
"3682148\n" | |
] | |
} | |
], | |
"source": [ | |
"# Los símbolos comparten el mismo object ID, pero las cadenas de texto no (aunque sea el mismo valor)\n", | |
"puts cadena1.object_id\n", | |
"puts cadena2.object_id\n", | |
"puts simbolo1.object_id\n", | |
"puts simbolo2.object_id" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "fdce18f3-ecab-41ff-bf39-ff852ab0f265", | |
"metadata": {}, | |
"source": [ | |
"# Métodos y argumentos" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 114, | |
"id": "120d181b-bbbd-4dbd-a51a-8f2950faf09f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"9\n", | |
"9\n" | |
] | |
} | |
], | |
"source": [ | |
"def square(x)\n", | |
" # retorna el cuadrado de un número\n", | |
" x * x\n", | |
"end\n", | |
"\n", | |
"puts square(3)\n", | |
"puts square 3 # los paréntesis de pueden ignorar, aunque se recomienda ponerlos a menos que uses un DSL" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 116, | |
"id": "d739ed28-fdb4-4d47-a777-0bc8541ecd5d", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hola mundo\n" | |
] | |
} | |
], | |
"source": [ | |
"# si no hay inputs, no es necsario poner paréntesis\n", | |
"def saludar\n", | |
" puts \"Hola mundo\"\n", | |
"end\n", | |
"saludar" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "6343897a-d3da-448e-b421-9156d1648a70", | |
"metadata": {}, | |
"source": [ | |
"* Por default, los mpetodos siempre retornan lo que hay en la última línea de código\n", | |
"* Podemos usar la palabra `return` para retornar un valor antes de que finalice la ejecución del programa" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 118, | |
"id": "7b8141f0-07de-416a-a932-b5499be33e82", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0" | |
] | |
}, | |
"execution_count": 118, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def square(x)\n", | |
" return 0 unless x.is_a? Integer\n", | |
" x * x\n", | |
"end\n", | |
"\n", | |
"square('jj')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "88e3b0a0-cc7c-4613-894c-7269eae1d9f7", | |
"metadata": {}, | |
"source": [ | |
"## Operador splat\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 122, | |
"id": "12a749f1-4e37-4400-8c04-ba8ad15fcc56", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hola Juan\n", | |
"Hola Lina\n", | |
"Hola Kelly\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[\"Juan\", \"Lina\", \"Kelly\"]" | |
] | |
}, | |
"execution_count": 122, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def hola_gente(personas)\n", | |
" personas.each {|persona| puts \"Hola #{persona}\"}\n", | |
"end\n", | |
"\n", | |
"# este método solo acepta arreglos o similares que tiene .each,\n", | |
"# pero no funcionaría con un entero o un string\n", | |
"# no es común en ruby usar un arreglo como input de un método\n", | |
"hola_gente(['Juan', 'Lina', 'Kelly'])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "5f3e0e04-9e7c-4766-809b-0263bf72c0c3", | |
"metadata": {}, | |
"source": [ | |
"* Con el splat operator ponemos un * en el input y ahora acepta cualquier argumento" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 130, | |
"id": "3dffd0c0-5ac8-4870-9b21-2112d0e1f1bf", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":hola_gente" | |
] | |
}, | |
"execution_count": 130, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def hola_gente(*personas)\n", | |
" personas.each {|persona| puts \"Hola #{persona}\"}\n", | |
"end\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 132, | |
"id": "fd0fb685-fb89-42d7-8cf1-828f823fa08b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hola 23\n", | |
"23\n", | |
"Hola jj\n", | |
"jj\n", | |
"Hola jj\n", | |
"Hola ls\n", | |
"jj\n", | |
"ls\n", | |
"Hola [\"jj\", \"ls\"]\n", | |
"jj\n", | |
"ls\n", | |
"Hola jj\n", | |
"Hola ls\n", | |
"jj\n", | |
"ls\n" | |
] | |
} | |
], | |
"source": [ | |
"puts hola_gente(23)\n", | |
"puts hola_gente(\"jj\")\n", | |
"puts hola_gente(\"jj\", 'ls')\n", | |
"puts hola_gente([\"jj\", 'ls'])\n", | |
"puts hola_gente(*[\"jj\", 'ls']) # también puedes usr splat como input, equivale a enviar cada elemento en lugar del arreglo" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "40e292a2-05a5-443a-989b-eea8f6006772", | |
"metadata": {}, | |
"source": [ | |
"## Keyword arguments" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 141, | |
"id": "7be9dc80-dd28-4659-9eea-fb05027d1a97", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hola señor Juan\n", | |
"Hola joven Juan\n", | |
"Hola joven \n" | |
] | |
} | |
], | |
"source": [ | |
"def hola(nombre: \"\", edad: 0)\n", | |
" if edad >= 30\n", | |
" puts \"Hola señor #{nombre}\"\n", | |
" elsif edad < 30\n", | |
" puts \"Hola joven #{nombre}\"\n", | |
" end\n", | |
"end\n", | |
"\n", | |
"hola(nombre: \"Juan\", edad: 40)\n", | |
"hola(nombre: \"Juan\") # valor por default edad = 0 \n", | |
"hola(edad: 15) # valor por default nombre \"\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "e628b7d7-ff5d-492c-820c-fad9b5b8947e", | |
"metadata": {}, | |
"source": [ | |
"* Podemos definir inputs opcionales con **" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 143, | |
"id": "4fde0680-5dd8-4a9f-a7eb-cfa264948874", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hola señor Juan\n", | |
"Cocodrilo\n" | |
] | |
} | |
], | |
"source": [ | |
"def hola(nombre: \"\", edad: 0, **options)\n", | |
" if edad >= 30\n", | |
" puts \"Hola señor #{nombre}\"\n", | |
" elsif edad < 30\n", | |
" puts \"Hola joven #{nombre}\"\n", | |
" end\n", | |
" \n", | |
" puts options[:animal_favorito]\n", | |
"end\n", | |
"\n", | |
"hola(nombre: \"Juan\", edad: 40, color_favorito: \"Azul\", animal_favorito: \"Cocodrilo\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "2b85814b-cb32-4195-84b5-778f97e85894", | |
"metadata": {}, | |
"source": [ | |
"* Podemos crear parámetros obligatorios si no ponemos un valor por default, por ejemplo, en el siguiente método siempre debe ir un input nombre." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 147, | |
"id": "3998e5dd-f48f-4cfc-af8d-7729a71d9162", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hola señor Juan\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"def hola(nombre:, edad: 0, **options)\n", | |
" if edad >= 30\n", | |
" puts \"Hola señor #{nombre}\"\n", | |
" elsif edad < 30\n", | |
" puts \"Hola joven #{nombre}\"\n", | |
" end\n", | |
" \n", | |
" puts options[:animal_favorito]\n", | |
"end\n", | |
"\n", | |
"hola(nombre: \"Juan\", edad: 40)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "664979b5-0cf5-4eb7-b799-2fc278aa9745", | |
"metadata": {}, | |
"source": [ | |
"# Clases y objetos\n", | |
"\n", | |
"* Se cran a partir de class\n", | |
"* Las clases se nombran usando Camelcase\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "cc13e2d7-99aa-4e2e-9e18-faf85452c992", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":stop" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"class Video\n", | |
" attr_accessor :minutes, :title # esto es un atributo\n", | |
" def play # esto es un método\n", | |
" end\n", | |
" \n", | |
" def pause\n", | |
" end\n", | |
" \n", | |
" def stop\n", | |
" end\n", | |
" \n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "2e1fc83f-4b02-4aa0-9529-435b02d256e4", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Objeto y clases\n", | |
"atributos\n" | |
] | |
} | |
], | |
"source": [ | |
"# Crando nuevos objetos Video\n", | |
"video_30 = Video.new\n", | |
"video_30.title = \"Objeto y clases\"\n", | |
"\n", | |
"video_31 = Video.new\n", | |
"video_31.title = \"atributos\"\n", | |
"\n", | |
"puts video_30.title\n", | |
"puts video_31.title" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "97c76a82-b281-42e6-9fc1-e6694f420c6d", | |
"metadata": {}, | |
"source": [ | |
"## Initialize\n", | |
"\n", | |
"* Método que se ejecuta cuando creamos una clase" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "22957cfb-e7cd-415b-9aca-a232f2d492d5", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":stop" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"class Video\n", | |
" attr_accessor :minutes, :title\n", | |
" \n", | |
" def initialize(title)\n", | |
" self.title = title\n", | |
" end\n", | |
" \n", | |
" def play\n", | |
" end\n", | |
" \n", | |
" def pause\n", | |
" end\n", | |
" \n", | |
" def stop\n", | |
" end\n", | |
" \n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"id": "bd058819-78ce-4b22-a0d1-064ea44cf634", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Objetos y clases\"" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"video_32 = Video.new(\"Objetos y clases\")\n", | |
"video_32.title" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "8ba82ca6-43db-438f-a834-edbb2f036730", | |
"metadata": {}, | |
"source": [ | |
"## Propiedades y métodos accesorios\n", | |
"* Las variables de instancia se crean con @ y el nombre. Estas variables no pueden leerse y definir su valor desde las instancias creadas, sino que solo se puede acceder a ellas desde dentro de la clase.\n", | |
"\n", | |
"* Puedes acceder a ellas desde cualquier método del objeto\n", | |
"\n", | |
"* Lo métodos para obtener una variable de instancia no se suelen llamar `get_atributo`, sino solo `atributo`, y los setters se definen como `atributo=`" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"id": "54b4ff69-fddd-4bc6-9e4f-9b5b1155ab59", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":nombre=" | |
] | |
}, | |
"execution_count": 28, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"class Tutor\n", | |
" def initialize(name)\n", | |
" @nombre = name # variables de instancia\n", | |
" end\n", | |
" \n", | |
" def say_my_name\n", | |
" puts @nombre\n", | |
" end\n", | |
" \n", | |
" def nombre\n", | |
" return @nombre\n", | |
" end\n", | |
" \n", | |
" def nombre=(nombre)\n", | |
" @nombre = nombre\n", | |
" end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"id": "75570c1b-e180-4a28-be0c-45058ba2e90d", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Juan\n", | |
"Javier\n" | |
] | |
} | |
], | |
"source": [ | |
"juan = Tutor.new(\"Juan\")\n", | |
"javier = Tutor.new(\"Javier\")\n", | |
"juan.say_my_name\n", | |
"javier.say_my_name\n", | |
"#juan.nombre # esto arrojaría error" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"id": "ba9527fa-1d85-4636-b7b7-582f0df3fc09", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Juan\n", | |
"juanito\n" | |
] | |
} | |
], | |
"source": [ | |
"puts juan.nombre\n", | |
"juan.nombre = \"juanito\"\n", | |
"puts juan.nombre" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "27df9bac-a397-4c68-8e98-1d8a637ab81f", | |
"metadata": {}, | |
"source": [ | |
"* Las variables se pueden definir usando `attr_accessor`, `attr_reader`, `attr_writer` que se encarga de definir los métodos getter y setter automáticamente:\n", | |
" * `attr_accessor`: define setter y getter\n", | |
" * `attr_reader`: solo define getter\n", | |
" * `attr_writer`: solo define setter" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"id": "6725512a-0584-4ff0-b70a-6d5c9d4d65db", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":initialize" | |
] | |
}, | |
"execution_count": 32, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"class Tutor\n", | |
" attr_accessor :nombre\n", | |
" \n", | |
" def initialize(name)\n", | |
" @nombre = name\n", | |
" end\n", | |
"\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"id": "336b7ab9-7b01-49d6-a656-6da3b8959713", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Juan\n", | |
"juanito\n" | |
] | |
} | |
], | |
"source": [ | |
"juan = Tutor.new(\"Juan\")\n", | |
"puts juan.nombre\n", | |
"juan.nombre = \"juanito\"\n", | |
"puts juan.nombre" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "4ea50c8b-bf98-428e-ac14-50d37cc39b7a", | |
"metadata": {}, | |
"source": [ | |
"## Herencia\n", | |
"\n", | |
"* Crear clases a partir de otras clases\n", | |
"* Solo se puede heredar de una sola clase" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"id": "8702b7e7-9082-4ac6-ae9d-1917ff99a2a4", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[:youtube_id, :youtube_id=]" | |
] | |
}, | |
"execution_count": 45, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"class Video\n", | |
" attr_accessor :title, :duration\n", | |
" \n", | |
" def initialize\n", | |
" end\n", | |
"end\n", | |
"\n", | |
"class YouTubeVideo < Video\n", | |
" attr_accessor :youtube_id\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"id": "55a561cf-ac91-41a0-be75-c3f2d976dd59", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Herencia en Ruby\"" | |
] | |
}, | |
"execution_count": 46, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"yt = YouTubeVideo.new\n", | |
"yt.title = \"Herencia en Ruby\"\n", | |
"yt.title" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "4a69cd37-97a2-49c6-8642-6f79a7cd8fb6", | |
"metadata": {}, | |
"source": [ | |
"* Se puede reescribir los métodos del padre" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 89, | |
"id": "68a24cdd-986a-4cae-9f84-dc5b7003cfe0", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":embed_video_code" | |
] | |
}, | |
"execution_count": 89, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"class Video\n", | |
" attr_accessor :title, :duration\n", | |
" \n", | |
" def initialize\n", | |
" end\n", | |
" \n", | |
" def embed_video_code\n", | |
" \"<video></video>\"\n", | |
" end\n", | |
"end\n", | |
"\n", | |
"class YouTubeVideo < Video\n", | |
" attr_accessor :youtube_id\n", | |
"\n", | |
" def embed_video_code # reemplaza el método de la clase padre\n", | |
" \"<iframe></iframe>\"\n", | |
" end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "f028f30a-3381-4acd-b54e-6a0a1f94e778", | |
"metadata": {}, | |
"source": [ | |
"* se puede reescribir el método conservando lo que el método padre hace agregando `super`" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 90, | |
"id": "51ac9a47-8c48-4166-be31-d1b9906c8a39", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":embed_video_code" | |
] | |
}, | |
"execution_count": 90, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"class Video\n", | |
" attr_accessor :title, :duration\n", | |
" \n", | |
" def initialize\n", | |
" end\n", | |
" \n", | |
" def embed_video_code\n", | |
" \"<video></video>\"\n", | |
" end\n", | |
"end\n", | |
"\n", | |
"class YouTubeVideo < Video\n", | |
" attr_accessor :youtube_id\n", | |
"\n", | |
" def embed_video_code\n", | |
" puts \"video embebido\"\n", | |
" super\n", | |
" end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 69, | |
"id": "efe0941f-2d32-4f11-a20a-7805c403e8ba", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"<video></video>\"" | |
] | |
}, | |
"execution_count": 69, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"video1 = Video.new\n", | |
"video1.embed_video_code" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 70, | |
"id": "0f82f3f4-ed14-4f6f-8b9f-f042f60566f1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"video embebido\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"\"<video></video>\"" | |
] | |
}, | |
"execution_count": 70, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"yt1 = YouTubeVideo.new\n", | |
"yt1.embed_video_code" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "52d5419e-1fbe-419e-a457-2e0ec17fe9d0", | |
"metadata": {}, | |
"source": [ | |
"* Implícitamente todas las clses heredan de la clase `Object`, a menos que se especifique una clase concreta. \n", | |
"\n", | |
"* La clase object tiene métodos y atributos definidos como `object_id`\n", | |
"\n", | |
"* Es posible modificar la clase Object, entonces todos los objetos creados tendrán las propiedades modificadas" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 72, | |
"id": "1a74dff6-c4cc-4304-ab89-e6025d244df7", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"72440" | |
] | |
}, | |
"execution_count": 72, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"YouTubeVideo.new.object_id" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "4833d195-dbf6-44f5-a4ea-45b46ef885ed", | |
"metadata": {}, | |
"source": [ | |
"## Alcance de métodos\n", | |
"\n", | |
"* Los métodos pueden ser públicos, privados o protegidos.\n", | |
" * Público: pueden ser llamados desde cualquier lugar. Por defecto todos son públicos.\n", | |
" * Privado: pueden llamarse solo desde dentro de la clase. Una clase hija hereda también los métodos privados.\n", | |
" * Protegido: puede llamarse desde otra clase siempre y cuando sean del mismo tipo." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 95, | |
"id": "05ae2c73-6058-4649-881b-55fcedea48e2", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":initialize" | |
] | |
}, | |
"execution_count": 95, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"class Humano\n", | |
"\n", | |
" def initialize\n", | |
" self.publico\n", | |
" self.privado # solo se puede acceder desde la clase\n", | |
" end\n", | |
"\n", | |
" def publico\n", | |
" puts \"Soy público\"\n", | |
" end\n", | |
"\n", | |
" private # todo lo que esté debajo será privado\n", | |
" def privado\n", | |
" puts \"Soy privado\"\n", | |
" end\n", | |
" \n", | |
" protected\n", | |
" def protegido\n", | |
" puts \"Soy protegido\"\n", | |
" end\n", | |
"end\n", | |
"\n", | |
"class Profesor < Humano\n", | |
" def initialize\n", | |
" self.publico\n", | |
" self.private\n", | |
" end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 99, | |
"id": "5b1d2c9a-a3a0-42f0-a29f-1d7a28873aa7", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Soy público\n", | |
"Soy privado\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"#<#<Class:0x0000028603eed078>::Humano:0x0000028604cb04d0>" | |
] | |
}, | |
"execution_count": 99, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"Humano.new\n", | |
"# Humano.new.privado # esto arrojará un error\n", | |
"# Humano.new.publico # esto sí se puede, porque es público" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "222c61d2-6a51-4573-84f3-c4c169033c54", | |
"metadata": {}, | |
"source": [ | |
"## Métodos de clase\n", | |
"* Las clases también son objetos\n", | |
"* Es como un método estático de clase. Anteponemos self. al crear el método\n", | |
"* Se puede llamar directamente sin instanciar un nuevo objeto\n", | |
"* Se usan cuando la funcionalidad no le pertenece a un objeto en particular, sino a la clase" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 106, | |
"id": "75b4f291-a8f2-4005-87b8-6b0baa510c9e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":nombre_clase=" | |
] | |
}, | |
"execution_count": 106, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"class SoyUnObjeto\n", | |
" @nombre_clase = \"SoyUnObjeto\" # variable de instancia\n", | |
" \n", | |
" def self.nombre_clase\n", | |
" @nombre_clase\n", | |
" end\n", | |
" \n", | |
" def self.nombre_clase=(nombre_clase)\n", | |
" @nombre_clase = nombre_clase\n", | |
" end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 107, | |
"id": "4c32c2df-5519-40b3-8f23-085b04eb5e55", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"SoyUnObjeto\n", | |
"Otro nombre de objeto\n", | |
"Otro nombre de objeto\n" | |
] | |
} | |
], | |
"source": [ | |
"puts SoyUnObjeto.nombre_clase\n", | |
"puts SoyUnObjeto.nombre_clase = \"Otro nombre de objeto\"\n", | |
"puts SoyUnObjeto.nombre_clase" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "a707bbd1-0aad-4071-8fc9-4532ae205e7b", | |
"metadata": {}, | |
"source": [ | |
"## Variables de clase\n", | |
"\n", | |
"* Se crean con @@\n", | |
"* Se pueden usar desde métodos de la clase o métodos del objeto\n", | |
"* Se usa cuando es una propiedad que pertenece a cada clase en lugar de a cada objeto." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 121, | |
"id": "2c265ee4-c816-4983-b15c-30d2d5afab64", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":test" | |
] | |
}, | |
"execution_count": 121, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"class Video\n", | |
" @tipo = \"Video/mp4\" # variable de instancia\n", | |
" @@type = \"Video/mp4\" # variable de clase\n", | |
" \n", | |
" def self.type_desde_clase\n", | |
" puts @@type\n", | |
" puts @tipo\n", | |
" end\n", | |
" \n", | |
" def type_desde_objeto\n", | |
" puts @@type\n", | |
" puts @tipo\n", | |
" end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 111, | |
"id": "685e8b38-6d5b-4042-a373-c925c4197094", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Video/mp4\n", | |
"Video/mp4\n" | |
] | |
} | |
], | |
"source": [ | |
"Video.type_desde_clase" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 119, | |
"id": "377e23b8-09fe-4fe4-9883-90d803e63077", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Video/mp4\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"# La variable @tipo aparece como nil, porque debe definirse para cada objeto particular\n", | |
"# en cambio @@type está en todos los objetos\n", | |
"Video.new.type_desde_objeto" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "e040dddd-aa6f-4358-b85b-8f1165f25244", | |
"metadata": {}, | |
"source": [ | |
"* Al heredar, sí se hereda la variable de clae, pero no la de instancia" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 131, | |
"id": "4646b518-672b-4d2b-b18b-3b02cfd5ac38", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class YouTube < Video\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 132, | |
"id": "3c82434d-29e4-443d-8c47-07af7205e04f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Video/mp4\n", | |
"Video/mp4\n" | |
] | |
} | |
], | |
"source": [ | |
"Video.type_desde_clase" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 133, | |
"id": "38c1f40c-6d16-434b-9cc7-843f47e8af57", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Video/mp4\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"YouTube.type_desde_clase" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "a2518e79-40d6-49fd-b606-75de65b3a0c5", | |
"metadata": {}, | |
"source": [ | |
"* Si cambias la variable de clase en cualquiera de las clases padre o hijas, el cambio se reflejará en todas" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 135, | |
"id": "0bb9ecf2-5ee1-4475-aae0-5cfbd1746d1b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Video/MPEG\"" | |
] | |
}, | |
"execution_count": 135, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"class YouTube < Video\n", | |
" @@type = \"Video/MPEG\"\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 138, | |
"id": "c9368e76-a030-44d4-910b-ea7af0532850", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Video/MPEG\n", | |
"Video/mp4\n", | |
"Video/MPEG\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"Video.type_desde_clase\n", | |
"YouTube.type_desde_clase" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "82b03f82-4709-480a-924a-013eff83d44e", | |
"metadata": {}, | |
"source": [ | |
"## Polimorfismo\n", | |
"* habilidad de que múltiples objetos actuen de formas distinta a ante el mismo mensaje" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 140, | |
"id": "e816a9e7-3243-476b-8393-ef62ae104bb8", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":play" | |
] | |
}, | |
"execution_count": 140, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"class Video\n", | |
" def play\n", | |
" # not implemented\n", | |
" end\n", | |
"end\n", | |
"\n", | |
"class Vimeo < Video\n", | |
" def play\n", | |
" puts \"inserta el reproductor de Vimeo\"\n", | |
" end\n", | |
"end\n", | |
"\n", | |
"class YouTube < Video\n", | |
" def play\n", | |
" puts \"inserta el reproductor de Youtube\"\n", | |
" end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 143, | |
"id": "51f8b68d-cc5d-4f59-baab-53269b75e021", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"inserta el reproductor de Youtube\n", | |
"inserta el reproductor de Vimeo\n", | |
"inserta el reproductor de Vimeo\n", | |
"inserta el reproductor de Youtube\n", | |
"inserta el reproductor de Youtube\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[#<#<Class:0x0000028603eed078>::YouTube:0x0000028603955098>, #<#<Class:0x0000028603eed078>::Vimeo:0x0000028603955048>, #<#<Class:0x0000028603eed078>::Vimeo:0x0000028603954ff8>, #<#<Class:0x0000028603eed078>::YouTube:0x0000028603954f80>, #<#<Class:0x0000028603eed078>::YouTube:0x0000028603954f08>]" | |
] | |
}, | |
"execution_count": 143, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"videos = [YouTube.new, Vimeo.new, Vimeo.new, YouTube.new, YouTube.new]\n", | |
"videos.each do |video|\n", | |
" video.play\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "9a500ebe-843f-484e-9c11-bfbe4c88e876", | |
"metadata": {}, | |
"source": [ | |
"# Métodos que reciben bloques" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 144, | |
"id": "10dce054-7fe0-4fd5-8905-bcf13522f2b7", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def hola\n", | |
"end\n", | |
"\n", | |
"# estoy enviando el bloque de código en {}, sin embargo no hace nada porque \n", | |
"# para ejecutarlo hace falta agregar yield al método\n", | |
"hola {puts \"Hola Juan\"} " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 146, | |
"id": "88128326-27c9-4452-8ca4-f71382e8ac11", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hola Juan\n" | |
] | |
} | |
], | |
"source": [ | |
"def hola\n", | |
" yield\n", | |
"end\n", | |
"\n", | |
"hola {puts \"Hola Juan\"} " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 147, | |
"id": "e4d0c750-5600-42d2-8f41-f5c9042f6ec5", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hola Juan\n" | |
] | |
} | |
], | |
"source": [ | |
"# también se puede ejecutar de esta manera\n", | |
"hola do\n", | |
" puts \"Hola Juan\"\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "4f530028-3b58-45d0-9e96-96a13632e60f", | |
"metadata": {}, | |
"source": [ | |
"* Si llamo solamente a al método `hola()` recibiré un error porque al poner yield espera recibior un bloque. Para evitar el error, se usa `block_given?`" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 153, | |
"id": "3a3d9098-8ea7-49f4-a49e-e5893b5daaef", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def hola\n", | |
" yield if block_given?\n", | |
"end\n", | |
"\n", | |
"hola" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 154, | |
"id": "b024994a-c56e-4696-9fee-467bf0461dfa", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hola Juan\n" | |
] | |
} | |
], | |
"source": [ | |
"hola {puts \"Hola Juan\"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 159, | |
"id": "fd3bdd07-f846-4fa1-998b-692bb74e3ce6", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hola Juan\n" | |
] | |
} | |
], | |
"source": [ | |
"# también se puede cambiar la sintaxis a:\n", | |
"def hola nombre, &bloque\n", | |
" bloque.call if block_given?\n", | |
"end\n", | |
"\n", | |
"hola(\"Javier\") {puts \"Hola Juan\"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 160, | |
"id": "5d8f9297-19f4-4598-91a2-299bc83ea193", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"hola(\"Javier\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "30e08e64-5df9-4f12-b0fd-4c5e29643a86", | |
"metadata": {}, | |
"source": [ | |
"* En general, usando `yield` es más rápida la ejecución" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "16964285-5c32-4965-9dc6-ebafa88f7e23", | |
"metadata": {}, | |
"source": [ | |
"## Argumento y valor retornado en bloque" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 162, | |
"id": "74928dd5-140d-48db-9b9b-a3d46ae6a9dc", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":saludar" | |
] | |
}, | |
"execution_count": 162, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"class Usuario\n", | |
" attr_accessor :nombre\n", | |
" \n", | |
" def saludar\n", | |
" yield(@nombre)\n", | |
" end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 163, | |
"id": "a17813e5-d780-4c7f-b018-2e8d30b61328", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hola Juan\n", | |
"Hello Juan\n" | |
] | |
} | |
], | |
"source": [ | |
"user = Usuario.new\n", | |
"user.nombre = \"Juan\"\n", | |
"\n", | |
"user.saludar {|nombre| puts \"Hola #{nombre}\"}\n", | |
"user.saludar {|nombre| puts \"Hello #{nombre}\"}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "f7877f13-2f85-43aa-8e40-13c3420be4a7", | |
"metadata": {}, | |
"source": [ | |
"* Se puede almacenar el bloque en una variable y usarlo" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 166, | |
"id": "4d9a679e-72e4-4898-b962-cd950938cc4c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":saludar" | |
] | |
}, | |
"execution_count": 166, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"class Usuario\n", | |
" attr_accessor :nombre\n", | |
" \n", | |
" def saludar\n", | |
" saludo = yield(@nombre)\n", | |
" puts saludo\n", | |
" end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 167, | |
"id": "52fd3daa-95f5-4ed9-83bb-bef2520d4275", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hola Juan\n", | |
"Hello Juan\n" | |
] | |
} | |
], | |
"source": [ | |
"user = Usuario.new\n", | |
"user.nombre = \"Juan\"\n", | |
"\n", | |
"user.saludar {|nombre| \"Hola #{nombre}\"}\n", | |
"user.saludar {|nombre| \"Hello #{nombre}\"}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "ab179aca-7bb1-44a8-be30-9fcbe5db4f61", | |
"metadata": {}, | |
"source": [ | |
"* No se puede usar return dentro de un bloque" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "3889993e-9151-48e6-b8f0-127cb72f44f4", | |
"metadata": { | |
"tags": [] | |
}, | |
"source": [ | |
"## Alcance de variables dentro de un bloque\n", | |
"\n", | |
"* Un bloque hereda el contexto en el cual es ejecutado" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 168, | |
"id": "10ebce67-3a99-416b-8b1e-c39c0db57d63", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hola juan\n" | |
] | |
} | |
], | |
"source": [ | |
"def hola\n", | |
" yield\n", | |
"end\n", | |
"\n", | |
"nombre = \"juan\"\n", | |
"hola {puts \"Hola #{nombre}\"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 170, | |
"id": "112c69d8-3667-464e-a36c-0aeda56c0aa5", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hola Javier\n" | |
] | |
} | |
], | |
"source": [ | |
"# dentro del bloque se pueden cambiar variables del contexto\n", | |
"hola do\n", | |
" nombre = \"Javier\"\n", | |
" hola {puts \"Hola #{nombre}\"}\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 173, | |
"id": "9aa0b165-e37e-4d0a-be11-2c7ed1401ca9", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hola Santos\n" | |
] | |
} | |
], | |
"source": [ | |
"# también se pueden definir variables locales solo existen dentro del bloque\n", | |
"# ; separa las variables input de variables locales creadas para el bloque\n", | |
"hola do |; apellido|\n", | |
" apellido = \"Santos\"\n", | |
" hola {puts \"Hola #{apellido}\"}\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "68299708-0df1-4649-8808-32ddd2c5a1f0", | |
"metadata": {}, | |
"source": [ | |
"# Proc\n", | |
"* Un bloque no es un objeto\n", | |
"* usando &block es un proc\n", | |
"* Los proc sí son objetos" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 175, | |
"id": "c89a78f6-1211-4261-877f-47ead19b877e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Proc\n", | |
"hola Juan\n" | |
] | |
} | |
], | |
"source": [ | |
"def hola &block\n", | |
" puts block.class.name\n", | |
" block.call\n", | |
"end\n", | |
"\n", | |
"hola {puts \"hola Juan\"}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "b4c36d4f-39a7-4109-98c9-e6dbef734f4e", | |
"metadata": {}, | |
"source": [ | |
"* Una función puede recibir un solo bloque, pero puede recibir varios proc" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 176, | |
"id": "d32d7331-ef9c-4eaf-ab06-1d71399ae9ae", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hola Juan\n", | |
"Hola Javier\n" | |
] | |
} | |
], | |
"source": [ | |
"def hola proc1, proc2\n", | |
" proc1.call\n", | |
" proc2.call\n", | |
"end\n", | |
"\n", | |
"proc1 = Proc.new {puts \"Hola Juan\"}\n", | |
"proc2 = Proc.new {puts \"Hola Javier\"}\n", | |
"\n", | |
"hola(proc1, proc2)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Ruby 3.1.2", | |
"language": "ruby", | |
"name": "ruby" | |
}, | |
"language_info": { | |
"file_extension": ".rb", | |
"mimetype": "application/x-ruby", | |
"name": "ruby", | |
"version": "3.1.2" | |
}, | |
"toc-autonumbering": true, | |
"toc-showcode": false, | |
"toc-showmarkdowntxt": false, | |
"toc-showtags": false | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment