-
Notifications
You must be signed in to change notification settings - Fork 8
/
about_control_statements.rb
142 lines (120 loc) · 2.72 KB
/
about_control_statements.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
require File.expand_path(File.dirname(__FILE__) + '/neo')
class AboutControlStatements < Neo::Koan
def test_if_then_else_statements
if true
result = :true_value
else
result = :false_value
end
assert_equal __, result
end
def test_if_then_statements
result = :default_value
if true
result = :true_value
end
assert_equal __, result
end
def test_if_statements_return_values
value = if true
:true_value
else
:false_value
end
assert_equal __, value
value = if false
:true_value
else
:false_value
end
assert_equal __, value
# NOTE: Actually, EVERY statement in Ruby will return a value, not
# just if statements.
end
def test_if_statements_with_no_else_with_false_condition_return_value
value = if false
:true_value
end
assert_equal __, value
end
def test_condition_operators
assert_equal __, (true ? :true_value : :false_value)
assert_equal __, (false ? :true_value : :false_value)
end
def test_if_statement_modifiers
result = :default_value
result = :true_value if true
assert_equal __, result
end
def test_unless_statement
result = :default_value
unless false # same as saying 'if !false', which evaluates as 'if true'
result = :false_value
end
assert_equal __, result
end
def test_unless_statement_evaluate_true
result = :default_value
unless true # same as saying 'if !true', which evaluates as 'if false'
result = :true_value
end
assert_equal __, result
end
def test_unless_statement_modifier
result = :default_value
result = :false_value unless false
assert_equal __, result
end
def test_while_statement
i = 1
result = 1
while i <= 10
result = result * i
i += 1
end
assert_equal __, result
end
def test_break_statement
i = 1
result = 1
while true
break unless i <= 10
result = result * i
i += 1
end
assert_equal __, result
end
def test_break_statement_returns_values
i = 1
result = while i <= 10
break i if i % 2 == 0
i += 1
end
assert_equal __, result
end
def test_next_statement
i = 0
result = []
while i < 10
i += 1
next if (i % 2) == 0
result << i
end
assert_equal __, result
end
def test_for_statement
array = ["fish", "and", "chips"]
result = []
for item in array
result << item.upcase
end
assert_equal [__, __, __], result
end
def test_times_statement
sum = 0
10.times do
sum += 1
end
assert_equal __, sum
end
end