File tree 12 files changed +220
-94
lines changed
tool/virtual_environment/docker
12 files changed +220
-94
lines changed Original file line number Diff line number Diff line change @@ -6,26 +6,35 @@ ref: https://docs.ruby-lang.org/en/3.4/Array.html
6
6
## Basic
7
7
8
8
``` ruby
9
+ my_array = Array .new
9
10
my_array = [1 , 2 , 3 ]
10
11
my_array = (1 ..10 ).to_a
11
12
12
13
my_array[1 ]
13
14
my_array.push(40 )
14
15
my_array << 40 # Same as push
15
-
16
16
```
17
17
18
18
## Methods
19
19
20
20
| Method | Description |
21
21
| ---------- | ----------------------------------------------------------------- |
22
22
| ` push ` | Appends each argument to the array. |
23
+ | ` max ` | Returns the biggest element in the array. |
24
+ | ` min ` | Returns the smallest element in the array |
25
+ | ` include? ` | Returns ` true ` if the array includes the specified element. |
23
26
| ` sort ` | Returns a new array sorted. |
24
27
| ` sort_by! ` | Sort in place by using the given block. |
25
28
| ` reverse ` | Returns a new array in inverse order. |
26
29
| ` map ` | Returns a new array with each element updated by the given block. |
27
30
| ` collect ` | Same as ` map ` . |
28
31
32
+ Check if array includes an element:
33
+
34
+ ``` ruby
35
+ my_array.include? ' critical'
36
+ ```
37
+
29
38
Update elements order:
30
39
31
40
``` ruby
Original file line number Diff line number Diff line change 12
12
13
13
## Functions
14
14
15
+ Simple printing:
16
+
15
17
``` ruby
16
- print " Hello" # Prints something and do NOT put a new line
17
- puts " Hello" # Prints something and puts a new line at the end
18
+ print ' Hello' # Prints something and do NOT put a new line
19
+ puts ' Hello' # Prints something and puts a new line at the end
20
+ ```
18
21
19
- puts " My name is #{ name } ! "
22
+ [ String formatting ] ( https://docs.ruby-lang.org/en/3.4/format_specifications_rdoc.html ) :
20
23
24
+ ``` ruby
25
+ sprintf (' %.1f' , 8.199 )
26
+ ```
27
+
28
+ Getting user input:
29
+
30
+ ``` ruby
21
31
my_name = gets # Gets the input from the user
22
32
my_name = gets .chomp # Same as gets but removes the new line
23
33
```
Original file line number Diff line number Diff line change 1
1
---
2
2
title : Control flow
3
+ ref : https://docs.ruby-lang.org/en/3.4/syntax/control_expressions_rdoc.html
3
4
---
4
5
5
6
## Condition
@@ -25,6 +26,64 @@ do_something unless cond
25
26
cond ? was_true : was_false
26
27
```
27
28
29
+ ## Loop
30
+
31
+ ``` ruby
32
+ loop do
33
+ next if cond # Same as continue, skip this iteration
34
+ do_something
35
+ break if cond
36
+ end
37
+ ```
38
+
39
+ ## For loop
40
+
41
+ ``` ruby
42
+ # # Include last number
43
+ for num in 1 ..10
44
+ puts num
45
+ end
46
+
47
+ # # Exclude last number
48
+ for num in 1 ...10
49
+ puts num
50
+ end
51
+ ```
52
+
53
+ ## While loop
54
+
55
+ ``` ruby
56
+ while cond
57
+ do_something
58
+ end
59
+
60
+ until cond
61
+ do_something
62
+ end
63
+ ```
64
+
65
+ ## Do while loop
66
+
67
+ ` do ` + ` end ` is the same as ` { ` + ` } ` .
68
+
69
+ ``` ruby
70
+ 95 .upto(100 ) do |num |
71
+ puts num
72
+ end
73
+
74
+ 95 .downto(90 ) do |num |
75
+ puts num
76
+ end
77
+
78
+ my_array.each do |item |
79
+ do_something
80
+ end
81
+
82
+ 10 .times do
83
+ do_something
84
+ end
85
+ ```
86
+
28
87
## Switch
29
88
30
89
``` ruby
Original file line number Diff line number Diff line change 1
1
---
2
- title : Basic types
2
+ title : Data types
3
3
---
4
4
5
5
## Types
6
6
7
7
``` ruby
8
- my_name = " Taro" # String
8
+ my_name = ' Taro' # String
9
9
my_int = 10 # Integer
10
10
my_float = 3.14
11
11
my_bool = true
12
12
my_nil = nil
13
13
my_symbol = :hello # Symbol
14
14
```
15
15
16
+ Check the type of a variable:
17
+
18
+ ``` ruby
19
+ my_var.class
20
+ ```
21
+
22
+ ## Type conversion
23
+
16
24
To convert between types:
17
25
18
26
``` ruby
19
27
my_name.to_sym # to symbol
20
28
my_name.intern # to symbol
21
29
my_symbol.to_s # to string
30
+ my_str.to_f # to float
22
31
```
23
32
24
- Number operations:
25
-
26
- ``` ruby
27
- my_int.next # Add 1 to an integer
28
- my_float.floor # Round down
29
- ```
33
+ ## Common methods
30
34
31
35
Check if an object can receive a method:
32
36
@@ -43,5 +47,5 @@ my_name.capitalize!
43
47
Methods that return boolean use ` ? ` :
44
48
45
49
``` ruby
46
- string_to_check.include? " substring"
50
+ string_to_check.include? ' substring'
47
51
```
Original file line number Diff line number Diff line change @@ -5,25 +5,41 @@ ref: https://docs.ruby-lang.org/en/3.4/Hash.html
5
5
6
6
## Basic
7
7
8
+ Create empty hash:
9
+
8
10
``` ruby
9
11
my_hash = Hash .new
10
- my_hash = Hash .new (" default value" )
11
- my_hash[" id" ] = 1
12
- my_hash.delete(key)
12
+ my_hash = {}
13
+ ```
14
+
15
+ Create filled hash:
13
16
17
+ ``` ruby
14
18
my_hash = {
15
- " name" => " Taro" ,
16
- " age" => 24
19
+ ' name' => ' Taro' ,
20
+ ' age' => 24 ,
17
21
}
18
22
my_hash = {
19
- :name => " Taro" ,
20
- :age => 24
23
+ :name => ' Taro' ,
24
+ :age => 24 ,
21
25
}
22
26
my_hash = {
23
- name: " Taro" ,
24
- age: 24
27
+ name: ' Taro' , # Becomes symbol
28
+ age: 24 , # Becomes symbol
25
29
}
30
+ ```
31
+
32
+ Access values of a hash:
26
33
34
+ ``` ruby
35
+ my_hash[' id' ] = 1
36
+ my_hash[:id ] = 1
37
+ my_hash.delete(key)
38
+ ```
39
+
40
+ ## Methods
41
+
42
+ ``` ruby
27
43
my_hash.select do |key , value |
28
44
value > 10
29
45
end
Original file line number Diff line number Diff line change
1
+ ---
2
+ title : CSV
3
+ ref : https://ruby.github.io/csv/
4
+ ---
5
+
6
+ ## Usage
7
+
8
+ Library that provides an interface to CSV files and data.
9
+
10
+ ``` rb
11
+ require ' csv'
12
+
13
+ CSV .foreach(' path/to/file.csv' , headers: true ) do |row |
14
+ puts row[' name' ]
15
+ end
16
+ ```
Original file line number Diff line number Diff line change @@ -3,9 +3,12 @@ title: Net::HTTP
3
3
ref : https://docs.ruby-lang.org/en/3.4/Net/HTTP.html
4
4
---
5
5
6
- ## Basic
6
+ ## Usage
7
+
8
+ Library to make HTTP requests.
7
9
8
10
``` ruby
9
11
require ' net/http'
12
+
10
13
Net ::HTTP .get(' example.com' , ' /index.html' )
11
14
```
Original file line number Diff line number Diff line change
1
+ ---
2
+ title : JSON
3
+ ref : https://docs.ruby-lang.org/en/3.4/JSON.html
4
+ ---
5
+
6
+ ## Usage
7
+
8
+ Library to generate or parse JSON.
9
+
10
+ ``` ruby
11
+ require ' json'
12
+ ```
13
+
14
+ Ruby object to JSON:
15
+
16
+ ``` ruby
17
+ my_hash = {
18
+ ' name' => ' Taro' ,
19
+ ' age' => 24 ,
20
+ }
21
+ json_str = my_hash.to_json
22
+ json_str = JSON .generate(my_hash)
23
+ ```
24
+
25
+ JSON to ruby object:
26
+
27
+ ``` ruby
28
+ my_hash = JSON .parse(json_str)
29
+ ```
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ ---
2
+ title : Numbers
3
+ ref : https://docs.ruby-lang.org/en/3.4/Numeric.html
4
+ ---
5
+
6
+ ## Methods
7
+
8
+ | Method | Description |
9
+ | ------- | --------------- |
10
+ | ` next ` | Increment by 1. |
11
+ | ` floor ` | Round down. |
You can’t perform that action at this time.
0 commit comments