Skip to content

Commit 6f7988c

Browse files
committed
Update ruby notes, small update to docker run note
1 parent 139ba08 commit 6f7988c

File tree

12 files changed

+220
-94
lines changed

12 files changed

+220
-94
lines changed

notes/language/ruby/array.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,35 @@ ref: https://docs.ruby-lang.org/en/3.4/Array.html
66
## Basic
77

88
```ruby
9+
my_array = Array.new
910
my_array = [1, 2, 3]
1011
my_array = (1..10).to_a
1112

1213
my_array[1]
1314
my_array.push(40)
1415
my_array << 40 # Same as push
15-
1616
```
1717

1818
## Methods
1919

2020
| Method | Description |
2121
| ---------- | ----------------------------------------------------------------- |
2222
| `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. |
2326
| `sort` | Returns a new array sorted. |
2427
| `sort_by!` | Sort in place by using the given block. |
2528
| `reverse` | Returns a new array in inverse order. |
2629
| `map` | Returns a new array with each element updated by the given block. |
2730
| `collect` | Same as `map`. |
2831

32+
Check if array includes an element:
33+
34+
```ruby
35+
my_array.include? 'critical'
36+
```
37+
2938
Update elements order:
3039

3140
```ruby

notes/language/ruby/builtin.md

+13-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,22 @@ irb
1212

1313
## Functions
1414

15+
Simple printing:
16+
1517
```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+
```
1821

19-
puts "My name is #{name}!"
22+
[String formatting](https://docs.ruby-lang.org/en/3.4/format_specifications_rdoc.html):
2023

24+
```ruby
25+
sprintf('%.1f', 8.199)
26+
```
27+
28+
Getting user input:
29+
30+
```ruby
2131
my_name = gets # Gets the input from the user
2232
my_name = gets.chomp # Same as gets but removes the new line
2333
```

notes/language/ruby/control_flow.md

+59
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
title: Control flow
3+
ref: https://docs.ruby-lang.org/en/3.4/syntax/control_expressions_rdoc.html
34
---
45

56
## Condition
@@ -25,6 +26,64 @@ do_something unless cond
2526
cond ? was_true : was_false
2627
```
2728

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+
2887
## Switch
2988

3089
```ruby
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
11
---
2-
title: Basic types
2+
title: Data types
33
---
44

55
## Types
66

77
```ruby
8-
my_name = "Taro" # String
8+
my_name = 'Taro' # String
99
my_int = 10 # Integer
1010
my_float = 3.14
1111
my_bool = true
1212
my_nil = nil
1313
my_symbol = :hello # Symbol
1414
```
1515

16+
Check the type of a variable:
17+
18+
```ruby
19+
my_var.class
20+
```
21+
22+
## Type conversion
23+
1624
To convert between types:
1725

1826
```ruby
1927
my_name.to_sym # to symbol
2028
my_name.intern # to symbol
2129
my_symbol.to_s # to string
30+
my_str.to_f # to float
2231
```
2332

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
3034

3135
Check if an object can receive a method:
3236

@@ -43,5 +47,5 @@ my_name.capitalize!
4347
Methods that return boolean use `?`:
4448

4549
```ruby
46-
string_to_check.include? "substring"
50+
string_to_check.include? 'substring'
4751
```

notes/language/ruby/hash.md

+25-9
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,41 @@ ref: https://docs.ruby-lang.org/en/3.4/Hash.html
55

66
## Basic
77

8+
Create empty hash:
9+
810
```ruby
911
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:
1316

17+
```ruby
1418
my_hash = {
15-
"name" => "Taro",
16-
"age" => 24
19+
'name' => 'Taro',
20+
'age' => 24,
1721
}
1822
my_hash = {
19-
:name => "Taro",
20-
:age => 24
23+
:name => 'Taro',
24+
:age => 24,
2125
}
2226
my_hash = {
23-
name: "Taro",
24-
age: 24
27+
name: 'Taro', # Becomes symbol
28+
age: 24, # Becomes symbol
2529
}
30+
```
31+
32+
Access values of a hash:
2633

34+
```ruby
35+
my_hash['id'] = 1
36+
my_hash[:id] = 1
37+
my_hash.delete(key)
38+
```
39+
40+
## Methods
41+
42+
```ruby
2743
my_hash.select do |key, value|
2844
value > 10
2945
end

notes/language/ruby/library/csv.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
```

notes/language/ruby/library/http.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ title: Net::HTTP
33
ref: https://docs.ruby-lang.org/en/3.4/Net/HTTP.html
44
---
55

6-
## Basic
6+
## Usage
7+
8+
Library to make HTTP requests.
79

810
```ruby
911
require 'net/http'
12+
1013
Net::HTTP.get('example.com', '/index.html')
1114
```

notes/language/ruby/library/json.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
```

notes/language/ruby/loop.md

-61
This file was deleted.

notes/language/ruby/numbers.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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. |

0 commit comments

Comments
 (0)