File tree Expand file tree Collapse file tree 3 files changed +139
-1
lines changed Expand file tree Collapse file tree 3 files changed +139
-1
lines changed Original file line number Diff line number Diff line change 3
3
- Ruby is dynamic, elegant syntax, simple and productivity.
4
4
- Creator is Yukihiro “Matz” Matsumoto.
5
5
- Philosophy for the design of Ruby:
6
+
6
7
> Ruby is designed to make programmers happy.
7
8
8
9
## Installation
Original file line number Diff line number Diff line change 1
1
# Variables
2
2
3
- ## Example :
3
+ ## Examples :
4
4
``` ruby
5
5
welcome = ' Welcome to Ruby Programming'
6
6
one = 1
Original file line number Diff line number Diff line change
1
+ # Conditionals
2
+
3
+ ## if
4
+
5
+ ``` ruby
6
+ if expression
7
+ code
8
+ end
9
+ ```
10
+ The code between * if* and * end* is executed when the * expression* evaluates to true.
11
+
12
+ Examples:
13
+ ``` ruby
14
+ # If x is greater than 5, increment x
15
+ x = 6
16
+ if x > 5
17
+ x += 1
18
+ end
19
+ x # => 7
20
+
21
+
22
+ if 10 > 5
23
+ puts " 10 is greater than 5"
24
+ end
25
+ ```
26
+
27
+ ## else
28
+ An * if* statement can include an ** else** clause to specify code to be executed if the condition is not true:
29
+
30
+ ``` ruby
31
+ if expression
32
+ code
33
+ else
34
+ code
35
+ end
36
+ ```
37
+
38
+ Examples:
39
+
40
+ ``` ruby
41
+ a = 10
42
+ b = 5
43
+
44
+ if a > b
45
+ puts " #{ a } is greater than #{ b } "
46
+ else
47
+ puts " #{ b } is greater than #{ a } "
48
+ end
49
+ ```
50
+
51
+ ## elsif
52
+ elsif is a shortened form of "else if"
53
+
54
+ ``` ruby
55
+ if expression_1
56
+ code
57
+ elsif expression_2
58
+ code
59
+ .
60
+ .
61
+ elsif expression_n
62
+ code
63
+ else
64
+ code
65
+ end
66
+ ```
67
+
68
+ Examples:
69
+ ``` ruby
70
+ if month < 6
71
+ price *= 0.8 # sale of 20%
72
+ elsif month == 6
73
+ price *= 0.7 # sale of 30%
74
+ else # months in 7..12
75
+ price *= 0.9 # sale of 10%
76
+ end
77
+ ```
78
+
79
+ ## unless
80
+
81
+ ``` ruby
82
+ unless expression
83
+ code
84
+ end
85
+ ```
86
+ Same with:
87
+ ``` ruby
88
+ if ! expression
89
+ code
90
+ end
91
+ ```
92
+ - * unless* is the opposite of * if* .
93
+ - * unless* executes code when expression evaluates to * false* or * nil* .
94
+
95
+ Examples:
96
+ ``` ruby
97
+ x = 3
98
+ unless x > 5 # That mean when x not greater than 5
99
+ x += 1 # Increment x
100
+ end
101
+ ```
102
+
103
+
104
+ ## case
105
+ * case* expression is an alternative to the if-elsif-else expression.
106
+
107
+ Examples:
108
+
109
+ ``` ruby
110
+ score = 9
111
+
112
+ case score
113
+ when 1 ..4
114
+ puts " Failed!"
115
+ when 5
116
+ puts " Pass!"
117
+ when 6 ..7
118
+ puts " Not bad!"
119
+ when 8 ..10
120
+ puts " Excellent!"
121
+ else
122
+ puts " Invalid score."
123
+ end
124
+ ```
125
+
126
+ ``` ruby
127
+ date = 5
128
+
129
+ case date
130
+ when 2 ,4 ,6
131
+ puts " Working in the morning"
132
+ when 3 ,5 ,7
133
+ puts " Working in the afternoon"
134
+ else
135
+ puts " Off"
136
+ end
137
+ ```
You can’t perform that action at this time.
0 commit comments