Created
June 5, 2021 07:32
-
-
Save rishiip/ff6584251269c5064f09fb00bbd4bfe2 to your computer and use it in GitHub Desktop.
Learning Basic Ruby
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": "7c4a9eda", | |
"metadata": {}, | |
"source": [ | |
"# Basic Ruby Notes" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "f9ee7e5d", | |
"metadata": {}, | |
"source": [ | |
"### Print" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "3af8503e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hello\n" | |
] | |
} | |
], | |
"source": [ | |
"puts \"Hello\";" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "f921de6a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hello\n" | |
] | |
} | |
], | |
"source": [ | |
"puts \"Hello\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "d9a2f927", | |
"metadata": {}, | |
"source": [ | |
"### Whitespace" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "bb8b013f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a = 1\n", | |
"b = 2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "d31f8f6d", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a + b # Interpreted as a+b: local variable" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "24509590", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a +b # Interpreted as a(+b): method call" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "dc3840d0", | |
"metadata": {}, | |
"source": [ | |
"### Line Endings" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "f806eec9", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Test +\n", | |
" continue line\n" | |
] | |
} | |
], | |
"source": [ | |
"puts \"Test +\n", | |
" continue line\";" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"id": "200852ac", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a +\n", | |
" b;" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "3c224fa9", | |
"metadata": {}, | |
"source": [ | |
"### Multiple lines" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"id": "47f3cc41", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" Creating a\n", | |
" multiple line string\n" | |
] | |
} | |
], | |
"source": [ | |
"print <<EOF\n", | |
" Creating a\n", | |
" multiple line string\n", | |
"EOF" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"id": "3f79665f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" Creating a\n", | |
" multiple line string\n" | |
] | |
} | |
], | |
"source": [ | |
"print <<\"EOF\";\n", | |
" Creating a\n", | |
" multiple line string\n", | |
"EOF" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"id": "b5370926", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"hi\n", | |
"hello\n" | |
] | |
} | |
], | |
"source": [ | |
"print <<`EOC` # executing commands\n", | |
" echo hi\n", | |
" echo hello\n", | |
"EOC" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"id": "ab0e454b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" print foo\n", | |
" print bar\n" | |
] | |
} | |
], | |
"source": [ | |
"print <<\"foo\", <<\"bar\"\n", | |
" print foo\n", | |
"foo\n", | |
" print bar\n", | |
"bar" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "2e3ff7a0", | |
"metadata": {}, | |
"source": [ | |
"### BEGIN Statement" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"id": "7aff79ea", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"begin\n", | |
"main\n" | |
] | |
} | |
], | |
"source": [ | |
"puts \"main\"\n", | |
"\n", | |
"BEGIN {\n", | |
" puts \"begin\"\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "f46b3784", | |
"metadata": {}, | |
"source": [ | |
"### Comments" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"id": "879ded89", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# One line comment" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"id": "7b8d47ef", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"=begin\n", | |
"one line\n", | |
"two lines\n", | |
"three lines\n", | |
"=end" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "b5362452", | |
"metadata": {}, | |
"source": [ | |
"### Variables" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "800d1577", | |
"metadata": {}, | |
"source": [ | |
" - local: starts with lowercase letter or _\n", | |
" - instance: starts with @\n", | |
" - class: starts with @@\n", | |
" - global: starts with $" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"id": "4c4f7bcd", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":total_people" | |
] | |
}, | |
"execution_count": 15, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"$team = \"Team\" # global\n", | |
"class Person\n", | |
" @@no_people = 0 # class\n", | |
" def initialize(id, name, age)\n", | |
" @id = id # instance\n", | |
" @name = name\n", | |
" @age = age\n", | |
" end\n", | |
" def print()\n", | |
" puts \"Id: #@id\\nName: #@name\\nAge: #@age\"\n", | |
" end\n", | |
" def total_people()\n", | |
" @@no_people += 1\n", | |
" puts \"Total people: #@@no_people\"\n", | |
" end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"id": "6dbcab8a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"#<Person:0x00007fb9a50154a8 @id=1, @name=\"Ana\", @age=23>" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"person = Person.new(1, \"Ana\", 23)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"id": "1d323506", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Id: 1\n", | |
"Name: Ana\n", | |
"Age: 23\n" | |
] | |
} | |
], | |
"source": [ | |
"person.print" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"id": "89ab6c1c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Total people: 1\n" | |
] | |
} | |
], | |
"source": [ | |
"person.total_people" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "7fc143a4", | |
"metadata": {}, | |
"source": [ | |
"### Constants" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"id": "b81b640a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Pi: 3.14\n" | |
] | |
} | |
], | |
"source": [ | |
"PI = 3.14\n", | |
"\n", | |
"puts \"Pi: #{PI}\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "f98412a0", | |
"metadata": {}, | |
"source": [ | |
"### Pseudo-Variables" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "a62ee9da", | |
"metadata": {}, | |
"source": [ | |
"- self: the receiver object of the current method\n", | |
"- true: boolean value\n", | |
"- false: boolean value\n", | |
"- nil: undefined value\n", | |
"- \\_\\_FILE__: current source file\n", | |
"- \\_\\_LINE__: current line number in the source file" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "23f6a0fa", | |
"metadata": {}, | |
"source": [ | |
"### Variable Types" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "053e8ef1", | |
"metadata": {}, | |
"source": [ | |
"- integer\n", | |
"- float\n", | |
"- string" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "c228a45e", | |
"metadata": {}, | |
"source": [ | |
"### Strings" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"id": "c79dac99", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Test\"" | |
] | |
}, | |
"execution_count": 20, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"Test\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"id": "c4fe35cb", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Test\"" | |
] | |
}, | |
"execution_count": 21, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"'Test'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"id": "a627ec80", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Test with ' apostrophe test\"" | |
] | |
}, | |
"execution_count": 22, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"'Test with \\' apostrophe test'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"id": "9b2d62a1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Test with / backslash test\"" | |
] | |
}, | |
"execution_count": 23, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"Test with /\\ backslash test\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"id": "9c0ea39e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Test \"\\\"\n" | |
] | |
} | |
], | |
"source": [ | |
"puts 'Test \"\\\\\"'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"id": "4280e622", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Test ' \n" | |
] | |
} | |
], | |
"source": [ | |
"puts 'Test \\' '" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"id": "b8484942", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Expression: 20\n" | |
] | |
} | |
], | |
"source": [ | |
"puts \"Expression: #{10+10}\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"id": "f256d693", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Hello World!\"" | |
] | |
}, | |
"execution_count": 27, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%{Hello World!}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"id": "38137510", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\" Hello World! \"" | |
] | |
}, | |
"execution_count": 28, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%Q{ Hello World! }" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"id": "336af4f3", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Hello World!\"" | |
] | |
}, | |
"execution_count": 29, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%q[Hello World!]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"id": "0ffcb6b8", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"learning_basic_ruby.ipynb\\n\"" | |
] | |
}, | |
"execution_count": 30, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%x!ls!" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"id": "f5d2a72b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"this is test\n" | |
] | |
} | |
], | |
"source": [ | |
"myStr = String.new(\"THIS IS TEST\")\n", | |
"test = myStr.downcase\n", | |
"\n", | |
"puts \"#{test}\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "1dfe292b", | |
"metadata": {}, | |
"source": [ | |
"### Arrays" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"id": "bcf2be67", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[\"Ana\", 23, 1.56]" | |
] | |
}, | |
"execution_count": 32, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"array = [\"Ana\", 23, 1.56]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"id": "5788b161", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[]" | |
] | |
}, | |
"execution_count": 33, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"array = Array.new" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"id": "4765ba3c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[nil, nil, nil, nil, nil]" | |
] | |
}, | |
"execution_count": 34, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"array = Array.new(5)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"id": "4972e31a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"5" | |
] | |
}, | |
"execution_count": 35, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"array.size" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"id": "bea40010", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"5" | |
] | |
}, | |
"execution_count": 36, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"array.length" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"id": "506ed3a6", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[\"Hello\", \"Hello\", \"Hello\", \"Hello\", \"Hello\"]" | |
] | |
}, | |
"execution_count": 37, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"strings = Array.new(5, \"Hello\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"id": "ec6e7244", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[0, 2, 4, 6, 8]" | |
] | |
}, | |
"execution_count": 38, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"even_numbers = Array.new(5) { |i| i = i * 2}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"id": "2b038873", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2, 3]" | |
] | |
}, | |
"execution_count": 39, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"nums = Array.[](1, 2, 3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"id": "94fb4a60", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2, 3]" | |
] | |
}, | |
"execution_count": 40, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"nums = Array[1, 2, 3]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"id": "d57a5f93", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" | |
] | |
}, | |
"execution_count": 41, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"digits = Array(0..9)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"id": "3e7e0de9", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"4" | |
] | |
}, | |
"execution_count": 42, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"test = [1, 2, 4, 5, 2]\n", | |
"test.at(2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"id": "a9feea83", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"4" | |
] | |
}, | |
"execution_count": 43, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"test[2]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "4e3b9f01", | |
"metadata": {}, | |
"source": [ | |
"### Hashes" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 44, | |
"id": "f3f3a3b8", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{\"name\"=>\"Ana\", \"age\"=>23, \"heigth\"=>1.56}" | |
] | |
}, | |
"execution_count": 44, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"person = {\"name\" => \"Ana\", \"age\" => 23, \"heigth\" => 1.56}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"id": "43651ea4", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{}" | |
] | |
}, | |
"execution_count": 45, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"days = Hash.new" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"id": "345abc61", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{}" | |
] | |
}, | |
"execution_count": 46, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"days = Hash.new( \"days\" )" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 47, | |
"id": "dea67556", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"days\"" | |
] | |
}, | |
"execution_count": 47, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"days[\"test\"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"id": "4c9b4fdc", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{}" | |
] | |
}, | |
"execution_count": 48, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"days = Hash.new \"days\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"id": "e041b249", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"days\"" | |
] | |
}, | |
"execution_count": 49, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"days[4]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"id": "4349fa77", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{\"sunday\"=>1, \"saturday\"=>7}" | |
] | |
}, | |
"execution_count": 50, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"weekends = Hash[\"sunday\" => 1, \"saturday\" => 7]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 51, | |
"id": "170ade5b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1" | |
] | |
}, | |
"execution_count": 51, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"weekends[\"sunday\"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 52, | |
"id": "3f1d5d2f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"7" | |
] | |
}, | |
"execution_count": 52, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"weekends[\"saturday\"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 53, | |
"id": "b5e9212b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[\"sunday\", \"saturday\"]" | |
] | |
}, | |
"execution_count": 53, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"weekends.keys" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "17567f19", | |
"metadata": {}, | |
"source": [ | |
"### Ranges" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 54, | |
"id": "ceb7f6d1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1..10" | |
] | |
}, | |
"execution_count": 54, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"(1..10)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 55, | |
"id": "bf7ac2a0", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1...10" | |
] | |
}, | |
"execution_count": 55, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"(1...10)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 56, | |
"id": "95b65cb4", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[\"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\"]" | |
] | |
}, | |
"execution_count": 56, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"('a'..'z').to_a" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 57, | |
"id": "e1f20593", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0..9" | |
] | |
}, | |
"execution_count": 57, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"digits = 0..9" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 58, | |
"id": "ec625a5a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"true" | |
] | |
}, | |
"execution_count": 58, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"digits.include?(5)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 59, | |
"id": "ef9dcba5", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0" | |
] | |
}, | |
"execution_count": 59, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"digits.min" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 60, | |
"id": "a23687cb", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"9" | |
] | |
}, | |
"execution_count": 60, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"digits.max" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 61, | |
"id": "6e798f72", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[5, 6, 7, 8, 9]" | |
] | |
}, | |
"execution_count": 61, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"digits.reject {|i| i < 5}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "0206031a", | |
"metadata": {}, | |
"source": [ | |
"### Iterators" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 62, | |
"id": "c35b0892", | |
"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" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"0..9" | |
] | |
}, | |
"execution_count": 62, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"digits.each do |digit|\n", | |
" puts digit\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 63, | |
"id": "6fa50dea", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" | |
] | |
}, | |
"execution_count": 63, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 64, | |
"id": "da188ffe", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"#<Enumerator: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:collect>" | |
] | |
}, | |
"execution_count": 64, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"b = Array.new\n", | |
"b = a.collect" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 65, | |
"id": "74d20516", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" | |
] | |
}, | |
"execution_count": 65, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"b.to_a" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 66, | |
"id": "50b6b3b8", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"10\n", | |
"20\n", | |
"30\n", | |
"40\n", | |
"50\n" | |
] | |
} | |
], | |
"source": [ | |
"a = [1,2,3,4,5]\n", | |
"b = a.collect{|x| 10*x}\n", | |
"puts b" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "1f7086ed", | |
"metadata": {}, | |
"source": [ | |
"### Operators" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "9e4719fb", | |
"metadata": {}, | |
"source": [ | |
"- Arithmetic: +, -, \\*, /, %, **\n", | |
"- Comparison: ==, !=, >, <, >=, <=, <=>, ===, .eql?, equal?\n", | |
"- Assignment: =, +=, -=, *=, /=, %=, **=\n", | |
"- Bitwise: &, |, ^, ~, <<, >>\n", | |
"- Logical: and, or, &&, ||, !, not\n", | |
"- Ternary: ?:\n", | |
"- Range: .., ...\n", | |
"- Defined: defined?" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "fba3161e", | |
"metadata": {}, | |
"source": [ | |
"### Class" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 67, | |
"id": "a22c2c06", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"23" | |
] | |
}, | |
"execution_count": 67, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"class Person\n", | |
" @@age = 23\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "82f50c1e", | |
"metadata": {}, | |
"source": [ | |
"### Object" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 68, | |
"id": "fb4a6115", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "ArgumentError", | |
"evalue": "wrong number of arguments (given 0, expected 3)", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[31mArgumentError\u001b[0m: wrong number of arguments (given 0, expected 3)", | |
"(iruby):4:in `initialize'", | |
"(iruby):1:in `new'", | |
"(iruby):1:in `<main>'" | |
] | |
} | |
], | |
"source": [ | |
"person = Person.new" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "9e4d192a", | |
"metadata": {}, | |
"source": [ | |
"### Custom Method to create objects" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 69, | |
"id": "c81d4d5b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":initialize" | |
] | |
}, | |
"execution_count": 69, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"class Person\n", | |
" @@no_people = 0\n", | |
" def initialize(id, name, age)\n", | |
" @id = id\n", | |
" @name = name\n", | |
" @age = age\n", | |
" end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 70, | |
"id": "528f774b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"#<Person:0x00007fb9a1977e50 @id=1, @name=\"Ana\", @age=23>" | |
] | |
}, | |
"execution_count": 70, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"person = Person.new(1, \"Ana\", 23)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "3e473c34", | |
"metadata": {}, | |
"source": [ | |
"### Member Function" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 71, | |
"id": "abdac703", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":sound" | |
] | |
}, | |
"execution_count": 71, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"class Cat\n", | |
" def sound\n", | |
" puts \"Meow\"\n", | |
" end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 72, | |
"id": "3ab9295b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Meow\n" | |
] | |
} | |
], | |
"source": [ | |
"cat = Cat.new\n", | |
"cat.sound" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "c3a5647d", | |
"metadata": {}, | |
"source": [ | |
"### Conditional" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 73, | |
"id": "9ff04c92", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"b is greater than a\n" | |
] | |
} | |
], | |
"source": [ | |
"# if...else statement\n", | |
"\n", | |
"a = 1\n", | |
"b = 2\n", | |
"\n", | |
"if a > b\n", | |
" puts \"a is greater than b\"\n", | |
"elsif a < b\n", | |
" puts \"b is greater than a\"\n", | |
"else\n", | |
" puts \"a is equal b\"\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 74, | |
"id": "9c3a42be", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"True\n" | |
] | |
} | |
], | |
"source": [ | |
"# if modifier\n", | |
"\n", | |
"value = true\n", | |
"\n", | |
"puts \"True\" if value" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 75, | |
"id": "8a2163bf", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"b is greater than a\n" | |
] | |
} | |
], | |
"source": [ | |
"# unless statement\n", | |
"\n", | |
"a = 1\n", | |
"b = 2\n", | |
"\n", | |
"unless a > b\n", | |
" puts \"b is greater than a\"\n", | |
"else\n", | |
" puts \"a is greater than b or a is equal b\"\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 76, | |
"id": "6662dae9", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"False\n" | |
] | |
} | |
], | |
"source": [ | |
"# unless modifier\n", | |
"\n", | |
"value = false\n", | |
"\n", | |
"puts \"False\" unless value" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 77, | |
"id": "5359f0a1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Out of range\n" | |
] | |
} | |
], | |
"source": [ | |
"# case statement\n", | |
"\n", | |
"number = 10\n", | |
"\n", | |
"case number\n", | |
"when 0 .. 2\n", | |
" puts \"Between 0..2\"\n", | |
"when 3 .. 5\n", | |
" puts \"Between 3..5\"\n", | |
"when 6 .. 9\n", | |
" puts \"Between 6..9\"\n", | |
"else\n", | |
" puts \"Out of range\"\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "a9e90823", | |
"metadata": {}, | |
"source": [ | |
"### Loops" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 78, | |
"id": "a06d6d17", | |
"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": [ | |
"# while statement\n", | |
"\n", | |
"i = 0\n", | |
"\n", | |
"while i < 10 do\n", | |
" puts i\n", | |
" i += 1\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 79, | |
"id": "39d8929b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"10\n" | |
] | |
} | |
], | |
"source": [ | |
"# while modifier\n", | |
"\n", | |
"i = 0\n", | |
"\n", | |
"i+=1 while i < 10\n", | |
"puts i" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 80, | |
"id": "55830607", | |
"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": [ | |
"# while modifier\n", | |
"\n", | |
"i = 0\n", | |
"\n", | |
"begin\n", | |
" puts i\n", | |
" i += 1\n", | |
"end while i < 10" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 81, | |
"id": "5cb78791", | |
"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", | |
"10\n" | |
] | |
} | |
], | |
"source": [ | |
"# until statement\n", | |
"\n", | |
"i = 0\n", | |
"\n", | |
"until i > 10 do\n", | |
" puts i\n", | |
" i += 1\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 82, | |
"id": "300bbe92", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"10\n" | |
] | |
} | |
], | |
"source": [ | |
"# until modifier\n", | |
"\n", | |
"i = 0\n", | |
"\n", | |
"i+=1 until i >= 10\n", | |
"puts i" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 83, | |
"id": "9baa88d7", | |
"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 modifier\n", | |
"\n", | |
"i = 0\n", | |
"\n", | |
"begin\n", | |
" puts i\n", | |
" i += 1\n", | |
"end until i >= 10" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 84, | |
"id": "0f8aa090", | |
"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", | |
"10\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"0..10" | |
] | |
}, | |
"execution_count": 84, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# for statement\n", | |
"\n", | |
"for i in 0..10\n", | |
" puts i\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 85, | |
"id": "bb883ade", | |
"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", | |
"10\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"0..10" | |
] | |
}, | |
"execution_count": 85, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# for each statement\n", | |
"\n", | |
"(0..10).each do |i|\n", | |
" puts i\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "a480251e", | |
"metadata": {}, | |
"source": [ | |
"- break: terminates the most internal loop\n", | |
"- next: jumps to the next iteration of the most internal loop\n", | |
"- redo: restarts the iteration of the most internal loop, whithout checking loop condition\n", | |
"- retry: restarts from the beginning or restarts the invocation" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "a2807c63", | |
"metadata": {}, | |
"source": [ | |
"### Methods" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 86, | |
"id": "245df62e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":person_method" | |
] | |
}, | |
"execution_count": 86, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def person_method(name=\"Ana\", age=23)\n", | |
" puts name, age\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 87, | |
"id": "3a23db53", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Ana\n", | |
"23\n" | |
] | |
} | |
], | |
"source": [ | |
"person_method" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 88, | |
"id": "6cd6b6b4", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Maria\n", | |
"22\n" | |
] | |
} | |
], | |
"source": [ | |
"person_method(\"Maria\", 22)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 89, | |
"id": "62ea9f0d", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":test_return_method" | |
] | |
}, | |
"execution_count": 89, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def test_return_method\n", | |
" a = 1\n", | |
" b = 2\n", | |
" c =3\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 90, | |
"id": "71bf1234", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3" | |
] | |
}, | |
"execution_count": 90, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"test_return_method" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 91, | |
"id": "23a9c836", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":test_no_return" | |
] | |
}, | |
"execution_count": 91, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def test_no_return\n", | |
" a = 1\n", | |
" b = 2\n", | |
" c = 3\n", | |
" return\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 92, | |
"id": "712cb03d", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"test_no_return" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 93, | |
"id": "bd7d3a26", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":test_one_return" | |
] | |
}, | |
"execution_count": 93, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def test_one_return\n", | |
" a = 1\n", | |
" b = 2\n", | |
" c = 3\n", | |
" return a\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 94, | |
"id": "8a33cabd", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1" | |
] | |
}, | |
"execution_count": 94, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"test_one_return" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 95, | |
"id": "2ca814e8", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":test_many_returns" | |
] | |
}, | |
"execution_count": 95, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def test_many_returns\n", | |
" a = 1\n", | |
" b = 2\n", | |
" c = 3\n", | |
" return a, b, c\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 96, | |
"id": "625d7f00", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2, 3]" | |
] | |
}, | |
"execution_count": 96, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"test_many_returns" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 97, | |
"id": "274120de", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":test_dinamic_parameters" | |
] | |
}, | |
"execution_count": 97, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def test_dinamic_parameters(*param)\n", | |
" for i in 0...param.length\n", | |
" puts param[i]\n", | |
" end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 98, | |
"id": "e8dc2304", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Ana\n", | |
"23\n", | |
"1.56\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"0...3" | |
] | |
}, | |
"execution_count": 98, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"test_dinamic_parameters \"Ana\", 23, 1.56" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 99, | |
"id": "59e81ba9", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":name" | |
] | |
}, | |
"execution_count": 99, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"class Person\n", | |
" def age(age)\n", | |
" puts age\n", | |
" end\n", | |
" def Person.name(name)\n", | |
" puts name\n", | |
" end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 100, | |
"id": "a7fee2a0", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Ana\n" | |
] | |
} | |
], | |
"source": [ | |
"Person.name(\"Ana\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 101, | |
"id": "957ac9f5", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "ArgumentError", | |
"evalue": "wrong number of arguments (given 0, expected 3)", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[31mArgumentError\u001b[0m: wrong number of arguments (given 0, expected 3)", | |
"(iruby):3:in `initialize'", | |
"(iruby):1:in `new'", | |
"(iruby):1:in `<main>'" | |
] | |
} | |
], | |
"source": [ | |
"person = Person.new\n", | |
"person.age(23)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 102, | |
"id": "7e7c82d3", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":hello_world" | |
] | |
}, | |
"execution_count": 102, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def hello_world\n", | |
" puts \"Hello World\"\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 103, | |
"id": "cb30565d", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"alias hello hello_world" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 104, | |
"id": "6aca2e0d", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hello World\n" | |
] | |
} | |
], | |
"source": [ | |
"hello" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 105, | |
"id": "13497f12", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"undef hello" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 106, | |
"id": "c963d7bf", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "NameError", | |
"evalue": "undefined local variable or method `hello' for main:Object", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[31mNameError\u001b[0m: undefined local variable or method `hello' for main:Object", | |
"(iruby):1:in `<main>'" | |
] | |
} | |
], | |
"source": [ | |
"hello" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "577726c1", | |
"metadata": {}, | |
"source": [ | |
"### Blocks" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 107, | |
"id": "53c05f6a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":test" | |
] | |
}, | |
"execution_count": 107, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def test\n", | |
" yield\n", | |
" puts \"Test\"\n", | |
" yield\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 108, | |
"id": "a6488bee", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hello\n", | |
"Test\n", | |
"Hello\n" | |
] | |
} | |
], | |
"source": [ | |
"test {puts \"Hello\"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 109, | |
"id": "dd75afb9", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":test_with_param" | |
] | |
}, | |
"execution_count": 109, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def test_with_param\n", | |
" yield 2\n", | |
" puts \"Test\"\n", | |
" yield 5\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 110, | |
"id": "eae6e075", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"2\n", | |
"Test\n", | |
"5\n" | |
] | |
} | |
], | |
"source": [ | |
"test_with_param {|i| puts i}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 111, | |
"id": "85f038ac", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":test_with_many_param" | |
] | |
}, | |
"execution_count": 111, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def test_with_many_param\n", | |
" yield 1, 2\n", | |
" puts \"Test\"\n", | |
" yield 5, 10\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 112, | |
"id": "e3c70dfc", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"2\n", | |
"Test\n", | |
"5\n", | |
"10\n" | |
] | |
} | |
], | |
"source": [ | |
"test_with_many_param {|i, j| puts i, j}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 113, | |
"id": "f3b2e650", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":test_block_method" | |
] | |
}, | |
"execution_count": 113, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def test_block_method(name, &print_name)\n", | |
" puts \"Test: #{name}\"\n", | |
" print_name.call\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 114, | |
"id": "2511caef", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Test: Ana\n", | |
"Name test\n" | |
] | |
} | |
], | |
"source": [ | |
"test_block_method(\"Ana\") {puts \"Name test\"}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "9af147d7", | |
"metadata": {}, | |
"source": [ | |
"### Modules and Mixins" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 115, | |
"id": "5e31b951", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":diameter" | |
] | |
}, | |
"execution_count": 115, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"module Circle\n", | |
" PI = 3.14\n", | |
" def circumference(r)\n", | |
" return 2*PI*r\n", | |
" end\n", | |
" def diameter(r)\n", | |
" return 2*r\n", | |
" end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 116, | |
"id": "692aa268", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"31.400000000000002" | |
] | |
}, | |
"execution_count": 116, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"include Circle \n", | |
"Circle.circumference(5)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 117, | |
"id": "86bc16b4", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":test_3" | |
] | |
}, | |
"execution_count": 117, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"module Test_1\n", | |
" def test_1\n", | |
" puts \"Test 1\"\n", | |
" end\n", | |
"end\n", | |
"\n", | |
"module Test_2\n", | |
" def test_2\n", | |
" puts \"Test 2\"\n", | |
" end\n", | |
"end\n", | |
"\n", | |
"class Test\n", | |
" include Test_1\n", | |
" include Test_2\n", | |
" def test_3\n", | |
" puts \"Test 3\"\n", | |
" end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 118, | |
"id": "286aa3f8", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Test 1\n", | |
"Test 2\n", | |
"Test 3\n" | |
] | |
} | |
], | |
"source": [ | |
"test = Test.new\n", | |
"test.test_1\n", | |
"test.test_2\n", | |
"test.test_3" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "bd8b06b7", | |
"metadata": {}, | |
"source": [ | |
"### Date and Time" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 119, | |
"id": "1d217371", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2021-06-03 21:31:12.239821 -0300" | |
] | |
}, | |
"execution_count": 119, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time_test = Time.new" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 120, | |
"id": "5399b5e8", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"2021-06-03 21:31:12.239821 -0300\"" | |
] | |
}, | |
"execution_count": 120, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time_test.inspect" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 121, | |
"id": "149f6d2c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2021-06-03 21:31:12.243344 -0300" | |
] | |
}, | |
"execution_count": 121, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"now = Time.now" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 122, | |
"id": "e89d8648", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"2021-06-03 21:31:12.243344 -0300\"" | |
] | |
}, | |
"execution_count": 122, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"now.inspect" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 123, | |
"id": "ce6ccac5", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2021-06-03 21:31:12.246706 -0300" | |
] | |
}, | |
"execution_count": 123, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time = Time.new" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 124, | |
"id": "a2e11831", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2021" | |
] | |
}, | |
"execution_count": 124, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.year" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 125, | |
"id": "27fc2a41", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"6" | |
] | |
}, | |
"execution_count": 125, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.month" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 126, | |
"id": "208a16cf", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3" | |
] | |
}, | |
"execution_count": 126, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.day" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 127, | |
"id": "d985e3e3", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"4" | |
] | |
}, | |
"execution_count": 127, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.wday" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 128, | |
"id": "44f08ebe", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"154" | |
] | |
}, | |
"execution_count": 128, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.yday" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 129, | |
"id": "0809ed5e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"21" | |
] | |
}, | |
"execution_count": 129, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.hour" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 130, | |
"id": "54db8bd1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"31" | |
] | |
}, | |
"execution_count": 130, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.min" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 131, | |
"id": "63ab1886", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"12" | |
] | |
}, | |
"execution_count": 131, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.sec" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 132, | |
"id": "d6e5abc3", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"246706" | |
] | |
}, | |
"execution_count": 132, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.usec" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 133, | |
"id": "fdafb77e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"-03\"" | |
] | |
}, | |
"execution_count": 133, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.zone" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 134, | |
"id": "b70e5133", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2021-06-03 00:00:00 -0300" | |
] | |
}, | |
"execution_count": 134, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"Time.local(2021, 6, 3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 135, | |
"id": "76e77827", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2021-06-03 12:30:00 -0300" | |
] | |
}, | |
"execution_count": 135, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"Time.local(2021, 6, 3, 12, 30)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 136, | |
"id": "ea171a97", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2021-06-03 12:30:30 -0300" | |
] | |
}, | |
"execution_count": 136, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"Time.local(2021, 6, 3, 12, 30, 30)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 137, | |
"id": "cea6c5d6", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[12, 31, 21, 3, 6, 2021, 4, 154, false, \"-03\"]" | |
] | |
}, | |
"execution_count": 137, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# [sec,min,hour,day,month,year,wday,yday,isdst,zone]\n", | |
"time.to_a" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 138, | |
"id": "cb8debef", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2021-06-03 21:31:12 UTC" | |
] | |
}, | |
"execution_count": 138, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"values = time.to_a\n", | |
"\n", | |
"Time.utc(*values)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 139, | |
"id": "b428ca37", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1622766672" | |
] | |
}, | |
"execution_count": 139, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# obj to sec\n", | |
"time = Time.now.to_i" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 140, | |
"id": "e4b86f92", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2021-06-03 21:31:12 -0300" | |
] | |
}, | |
"execution_count": 140, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# sec to obj\n", | |
"Time.at(time)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 141, | |
"id": "d41e49f3", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1622766672.27322" | |
] | |
}, | |
"execution_count": 141, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# microsec\n", | |
"Time.now.to_f" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 142, | |
"id": "ddc35732", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2021-06-03 21:31:12.276579 -0300" | |
] | |
}, | |
"execution_count": 142, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time = Time.new" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 143, | |
"id": "fe31aef7", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"-03\"" | |
] | |
}, | |
"execution_count": 143, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.zone" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 144, | |
"id": "e61303c2", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"-10800" | |
] | |
}, | |
"execution_count": 144, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.utc_offset" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 145, | |
"id": "46b78730", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"false" | |
] | |
}, | |
"execution_count": 145, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.isdst" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 146, | |
"id": "40236c84", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"false" | |
] | |
}, | |
"execution_count": 146, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.utc?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 147, | |
"id": "0bbc2b58", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2021-06-03 21:31:12.276579 -0300" | |
] | |
}, | |
"execution_count": 147, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.localtime" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 148, | |
"id": "bdb19833", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2021-06-04 00:31:12.276579 UTC" | |
] | |
}, | |
"execution_count": 148, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.gmtime" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 149, | |
"id": "d1738126", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2021-06-03 21:31:12.276579 -0300" | |
] | |
}, | |
"execution_count": 149, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.getlocal" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 150, | |
"id": "e68cdd52", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2021-06-04 00:31:12.276579 UTC" | |
] | |
}, | |
"execution_count": 150, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.getutc" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 151, | |
"id": "84220797", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"2021-06-04 00:31:12 UTC\"" | |
] | |
}, | |
"execution_count": 151, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.to_s" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 152, | |
"id": "d7ef29a0", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Fri Jun 4 00:31:12 2021\"" | |
] | |
}, | |
"execution_count": 152, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.ctime" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 153, | |
"id": "26a87b6c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"2021/06/04 00:31:12\"" | |
] | |
}, | |
"execution_count": 153, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"time.strftime(\"%Y/%m/%d %H:%M:%S\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 154, | |
"id": "76933c8f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2021-06-03 21:31:12.294691 -0300" | |
] | |
}, | |
"execution_count": 154, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"now = Time.now" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 155, | |
"id": "4a998cf2", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2021-06-03 20:31:12.294691 -0300" | |
] | |
}, | |
"execution_count": 155, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"past = now - 3600" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 156, | |
"id": "7cb7a033", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2021-06-03 22:31:12.294691 -0300" | |
] | |
}, | |
"execution_count": 156, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"future = now + 3600" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 157, | |
"id": "060704a6", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"7200.0" | |
] | |
}, | |
"execution_count": 157, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"diff = future - past" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "8ce9445c", | |
"metadata": {}, | |
"source": [ | |
"### File I/O" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 158, | |
"id": "d860635f", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Print -> puts\n", | |
"# Input -> gets" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 159, | |
"id": "cd947a61", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "Errno::ENOENT", | |
"evalue": "No such file or directory @ rb_sysopen - test.txt", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[31mErrno::ENOENT\u001b[0m: No such file or directory @ rb_sysopen - test.txt", | |
"(iruby):1:in `initialize'", | |
"(iruby):1:in `new'", | |
"(iruby):1:in `<main>'" | |
] | |
} | |
], | |
"source": [ | |
"file = File.new(\"test.txt\", \"r+\")\n", | |
"if file\n", | |
" file.syswrite(\"Teste\\n\")\n", | |
" file.syswrite(\"Teste 2\")\n", | |
" file.each_byte {|ch| putc ch; putc ?. }\n", | |
"else\n", | |
" puts \"Unable to open file!\"\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 160, | |
"id": "ba7f1ee2", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "Errno::ENOENT", | |
"evalue": "No such file or directory @ rb_sysopen - test.txt", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[31mErrno::ENOENT\u001b[0m: No such file or directory @ rb_sysopen - test.txt", | |
"(iruby):1:in `readlines'", | |
"(iruby):1:in `<main>'" | |
] | |
} | |
], | |
"source": [ | |
"test = IO.readlines(\"test.txt\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 161, | |
"id": "f3d732f1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "Errno::ENOENT", | |
"evalue": "No such file or directory @ rb_sysopen - test.txt", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[31mErrno::ENOENT\u001b[0m: No such file or directory @ rb_sysopen - test.txt", | |
"(iruby):1:in `foreach'", | |
"(iruby):1:in `<main>'" | |
] | |
} | |
], | |
"source": [ | |
"IO.foreach(\"test.txt\"){|block| puts block}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 162, | |
"id": "6e15d180", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "Errno::ENOENT", | |
"evalue": "No such file or directory @ rb_file_s_rename - (test.txt, test2.txt)", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[31mErrno::ENOENT\u001b[0m: No such file or directory @ rb_file_s_rename - (test.txt, test2.txt)", | |
"(iruby):1:in `rename'", | |
"(iruby):1:in `<main>'" | |
] | |
} | |
], | |
"source": [ | |
"File.rename( \"test.txt\", \"test2.txt\" )" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 163, | |
"id": "efe9a64e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "Errno::ENOENT", | |
"evalue": "No such file or directory @ apply2files - test2.txt", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[31mErrno::ENOENT\u001b[0m: No such file or directory @ apply2files - test2.txt", | |
"(iruby):1:in `delete'", | |
"(iruby):1:in `<main>'" | |
] | |
} | |
], | |
"source": [ | |
"File.delete(\"test2.txt\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 164, | |
"id": "0bff9663", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"File.open(\"test.txt\") if File::exists?( \"tests.txt\" )" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 165, | |
"id": "1f82ceb7", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"false" | |
] | |
}, | |
"execution_count": 165, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"File.file?( \"test.txt\" )" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 166, | |
"id": "3bd4bbc1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1" | |
] | |
}, | |
"execution_count": 166, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"require 'tmpdir'\n", | |
" tempfilename = File.join(Dir.tmpdir, \"tingtong\")\n", | |
" tempfile = File.new(tempfilename, \"w\")\n", | |
" tempfile.puts \"This is a temporary file\"\n", | |
" tempfile.close\n", | |
" File.delete(tempfilename)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 167, | |
"id": "ba760418", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"/var/folders/v0/8wvzzyw52vv3dtlp1zrpy8lr0000gn/T/tingtong20210603-6906-9c69wi\n" | |
] | |
} | |
], | |
"source": [ | |
"require 'tempfile'\n", | |
" f = Tempfile.new('tingtong')\n", | |
" f.puts \"Hello\"\n", | |
" puts f.path\n", | |
" f.close" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "f9ab858e", | |
"metadata": {}, | |
"source": [ | |
"### Exceptions" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 168, | |
"id": "d7321b84", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"#<IO:0x00007fb99d89a4c8>==#<IO:0x00007fb99d89a4c8>\n" | |
] | |
} | |
], | |
"source": [ | |
"begin\n", | |
" file = open(\"/unexistant_file\")\n", | |
" if file\n", | |
" puts \"File opened successfully\"\n", | |
" end\n", | |
"rescue\n", | |
" file = STDIN\n", | |
"end\n", | |
"print file, \"==\", STDIN, \"\\n\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 169, | |
"id": "c574b179", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"A test exception.\n" | |
] | |
} | |
], | |
"source": [ | |
"begin \n", | |
" raise 'A test exception.' \n", | |
"rescue Exception => e \n", | |
" puts e.message\n", | |
"end " | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Ruby 3.0.1", | |
"language": "ruby", | |
"name": "ruby" | |
}, | |
"language_info": { | |
"file_extension": ".rb", | |
"mimetype": "application/x-ruby", | |
"name": "ruby", | |
"version": "3.0.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment