Skip to content

Commit 343652b

Browse files
committed
Update content of sections
1 parent 3e58ca4 commit 343652b

13 files changed

+368
-275
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/

README.md

Lines changed: 73 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,95 @@
1-
## Ruby Programming Tutorial
1+
# Ruby Programming Tutorial
22
This tutorial help beginners easy to learn and approach Ruby step by step. Have solid basic to learn about Ruby on Rails.
33

4-
### What Will I Learn?
4+
## What Will I Learn?
55

66
- Features, data types of Ruby.
77
- Object-Oriented Programming in Ruby.
88
- You’ll be able to code by Ruby programming language.
99

1010

11-
### Table of contents
11+
## Table of contents
1212

13-
- [Getting Started](/getting_started.md)
14-
- Introduction
15-
- Installation
16-
- Running Ruby
17-
- Official Documents
13+
### [Getting Started](/getting_started.md)
14+
- Introduction
15+
- Installation
16+
- Running Ruby
17+
- Official Documents
1818

19-
- [Variables](/variables.md)
20-
- What is a variable?
21-
- Examples
19+
### [Variables](/variables.md)
20+
- What is a variable?
21+
- Examples
2222

23-
- [Constants]()
24-
- What is a constant?
25-
- Example
26-
- Best Pratise
27-
28-
- [Comments]()
29-
- What is a comment?
30-
- Single line comments
31-
- Multiline comments
23+
### [Constants](/constants.md)
24+
- What is a constant?
25+
- Example
26+
- Tips
3227

33-
- [Strings](/strings.md)
34-
- What is a String in Ruby?
35-
- Interpolation
36-
- Indexing
37-
- Common methods
28+
### [Comments](/comments.md)
29+
- What is a comment?
30+
- Single line comments
31+
- Multiline comments
3832

39-
- [Numbers](/numbers.md)
40-
- Integers
41-
- Floats
42-
- Operaters
43-
- Convert numbers
44-
45-
- [True, False and Nil](/true_false_nil.md)
46-
- true and false are the two Boolean values, and they represent truth and falsehood, yes and no, on and off.
47-
- nil is a special value to indicate the absence of value.
33+
### [Strings](/strings.md)
34+
- What is a String in Ruby?
35+
- Interpolation
36+
- Indexing
37+
- Common methods
38+
39+
### [Numbers](/numbers.md)
40+
- Integers
41+
- Floats
42+
- Operaters
43+
- Convert numbers
4844

49-
- [Symbols](/symbols.md)
50-
- What is a symbol in Ruby?
51-
- Examples
52-
- Common methods
45+
### [True, False and Nil](/true_false_nil.md)
46+
- true and false are the two Boolean values, and they represent truth and falsehood, yes and no, on and off.
47+
- nil is a special value to indicate the absence of value.
5348

49+
### [Symbols](/symbols.md)
50+
- What is a symbol in Ruby?
51+
- Examples
52+
- Common methods
5453

55-
- [Date & Time]()
54+
### Date & Time
5655

57-
- [Arrays](/arrays.md)
58-
- Create an Array
59-
- Indexing
60-
- Accessing elements
61-
- Iteration
62-
- Common methods
56+
### [Arrays](/arrays.md)
57+
- Create an Array
58+
- Indexing
59+
- Accessing elements
60+
- Iteration
61+
- Common methods
6362

64-
- [Range]()
63+
### Range
6564

66-
- [Hashes](/hashes.md)
67-
- Create a Hash
68-
- Accessing value by key
69-
- Iteration
70-
- Common Uses
65+
### [Hashes](/hashes.md)
66+
- Create a Hash
67+
- Accessing value by key
68+
- Iteration
69+
- Common Uses
7170

72-
- [Control Flow](/conditional.md)
73-
- if
74-
- else
75-
- elsif
76-
- unless
77-
- case
78-
79-
- [Blocks]()
80-
- What is a blocks in Ruby?
81-
- Examples
82-
83-
- [Methods]()
84-
- Defining a method
85-
- Default parameters in method
71+
### [Control Flow](/conditional.md)
72+
- if
73+
- else
74+
- elsif
75+
- unless
76+
- case
8677

87-
- [Classes]()
88-
- Defining a Class
89-
- Constructor
90-
- Accessing methods levels
91-
- Accessing instance variables
78+
### [Blocks]
79+
- What is a blocks in Ruby?
80+
- Examples
9281

93-
- [Module]()
94-
- Mixin
95-
- Module as Namespace
96-
- Modules and Class Composition
82+
### Methods
83+
- Defining a method
84+
- Default parameters in method
85+
86+
### Classes
87+
- Defining a Class
88+
- Constructor
89+
- Accessing methods levels
90+
- Accessing instance variables
91+
92+
### Module
93+
- Mixin
94+
- Module as Namespace
95+
- Modules and Class Composition

arrays.md

Lines changed: 115 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,40 @@ array_3 << 3 # Insert 3 to array_3
1414
=> [1, 2, 3]
1515
```
1616

17-
## Index
17+
## Indexing
18+
- Array indexing start at 0 for first element, the second gets 1, and so on.
19+
- An index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on.
1820

1921
```ruby
2022
Array: [5, 2, 4, 7, 9]
2123
| | | | |
2224
Index: 0 1 2 3 4
2325
```
24-
That mean: The first element gets the index 0. The second gets 1, and so on.
2526

2627
```ruby
2728
array = [5, 2, 4, 7, 9]
28-
array[0] # => 5
29-
array[1] # => 2
30-
array[4] # => 9
29+
array[0] # => 5
30+
array[1] # => 2
31+
array[4] # => 9
32+
array[-1] # => 9
3133
```
3234

33-
## Common methods
35+
## Accessing elements
36+
3437
- Get first element in array
3538
```ruby
3639
array = [5, 2, 4, 7, 9]
37-
array[0] # => 5
38-
array.first # => 5
39-
40+
array[0] # => 5
41+
array.first # => 5
4042
```
4143

4244
- Get last element in array
4345
```ruby
4446
array = [5, 2, 4, 7, 9]
45-
array.last # => 9
46-
array[4] # => 9
47-
array[-1] # => 9
47+
array.last # => 9
48+
array[4] # => 9
49+
array[-1] # => 9
50+
4851
```
4952

5053
- Get second of last element in array
@@ -53,10 +56,109 @@ array = [5, 2, 4, 7, 9]
5356
array[-2] # => 7
5457
```
5558

56-
- Check a particular value in array
59+
- Get first n elements in array
60+
```ruby
61+
array = [5, 2, 4, 7, 9]
62+
array.first(3) # => [5, 2, 4]
63+
```
64+
65+
- Get last n elements in array
5766
```ruby
5867
array = [5, 2, 4, 7, 9]
68+
array.last(2) # => [7, 9]
69+
```
70+
71+
## Iteration
72+
### each
73+
74+
You can iterate elements in array:
75+
```ruby
76+
[3, 4, 5].each{ |i| puts "element: #{i}" }
77+
# Output:
78+
element: 3
79+
element: 4
80+
element: 5
81+
=> [3, 4, 5]
82+
```
83+
84+
You can iterate over nested arrays:
85+
```ruby
86+
[[1, 2], [3, 4], [7, 8]].each do |a, b|
87+
puts "a: #{a}, b: #{b}"
88+
end
89+
# Output:
90+
a: 1, b: 2
91+
a: 3, b: 4
92+
a: 7, b: 8
93+
=> [[1, 2], [3, 4], [7, 8]]
94+
```
95+
96+
### each_with_index
97+
You can iterate elements with index in array:
98+
99+
```ruby
100+
[3, 4, 5].each_with_index do |element, index|
101+
puts "Index #{index}: element is #{element}"
102+
end
103+
# Output
104+
Index 0: element is 3
105+
Index 1: element is 4
106+
Index 2: element is 5
107+
```
108+
109+
## Filtering with *select*, *reject*
59110

111+
- *select*: will returen elements match to the condition
112+
```ruby
113+
array = [5, 2, 4, 7, 9]
114+
array.select{|item| item > 5} # => [7, 9]
115+
```
116+
117+
- *reject*: will returen elements do not match to the condition
118+
```ruby
119+
array = [5, 2, 4, 7, 9]
120+
array.reject{|item| item > 5} # => [5, 2, 4]
121+
```
122+
123+
## map:
124+
Create new array by invoke a block on each element of array.
125+
126+
```ruby
127+
array = [1, 2, 3]
128+
array.map{|item| item * 2} # => [2, 4, 6]
129+
array.map{|item| item.to_s} # => ["1", "2", "3"]
130+
array.map(&:to_s) # => ["1", "2", "3"]
131+
```
132+
133+
## Common methods
134+
135+
- Count numbers of elements of array with #count, #length or #size
136+
137+
```ruby
138+
array = [1, 2, 4, 7, 9]
139+
array.count # => 5
140+
array.length # => 5
141+
array.size # => 5
142+
```
143+
144+
- Remove all nil elements in array with #compact and #compact!
145+
146+
```ruby
147+
array = [5, nil, 4, 7, 9, nil]
148+
# It return new copy array with nil removed
149+
# Without effecting the origin array
150+
array.compact #=> [5, 4, 7, 9]
151+
array #=> [5, nil, 4, 7, 9, nil]
152+
153+
# It effect to origin array
154+
array.compact! #=> [5, 4, 7, 9]
155+
array #=> [5, 4, 7, 9]
156+
```
157+
158+
- Check a particular value in array with #include?
159+
160+
```ruby
161+
array = [5, 2, 4, 7, 9]
60162
array.include?(4)
61163
=> true
62164

comments.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Comments
2+
3+
## What is a comment?
4+
- Are line of annotations within Ruby code that are ignored at runtime.
5+
- We use comment to make source code easier to understand.
6+
7+
## Single line comments
8+
A single line comment start with # character
9+
10+
```ruby
11+
# This is a single line comment
12+
puts 'Welcome to comment section'
13+
```
14+
15+
When executed, output of above program will *Welcome to comment section*
16+
17+
## Multiline comments
18+
Multiline comments using by =begin and =end syntax
19+
20+
```ruby
21+
=begin
22+
This is a multiline comment
23+
Write your comment here
24+
=end
25+
puts 'Welcome to comment section'
26+
```
27+

0 commit comments

Comments
 (0)