-
Notifications
You must be signed in to change notification settings - Fork 0
/
about_control_statements.rb
116 lines (98 loc) · 2.3 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
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
class AboutControlStatements < EdgeCase::Koan
def test_if_then_else_statements
if true
result = :true_value
else
result = :false_value
end
assert_equal :true_value, result
end
def test_if_then_statements
result = :default_value
if true
result = :true_value
end
assert_equal :true_value, result
end
def test_if_statements_return_values
value = if true
:true_value
else
:false_value
end
assert_equal :true_value, value
value = if false
:true_value
else
:false_value
end
assert_equal :false_value, 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 nil, value
end
def test_condition_operators
assert_equal :true_value, (true ? :true_value : :false_value)
assert_equal :false_value, (false ? :true_value : :false_value)
end
def test_if_statement_modifiers
result = :default_value
result = :true_value if true
assert_equal :true_value, result
end
def test_unless_statement
result = :default_value
unless false
result = :false_value
end
assert_equal :false_value, result
end
def test_unless_statement_modifier
result = :default_value
result = :false_value unless false
assert_equal :false_value, result
end
def test_while_statement
i = 1
result = 1
while i <= 10
result = result * i
i += 1
end
assert_equal 3628800, result
end
def test_break_statement
i = 1
result = 1
while true
break unless i <= 10
result = result * i
i += 1
end
assert_equal 3628800, result
end
def test_next_statement
i = 0
result = []
while i < 10
i += 1
next if (i % 2) == 0
result << i
end
assert_equal [1,3,5,7,9], result
end
def test_for_statement
array = ["fish", "and", "chips"]
result = []
for item in array
result << item.upcase
end
assert_equal ['FISH', 'AND', 'CHIPS'], result
end
end