-
Notifications
You must be signed in to change notification settings - Fork 0
/
about_strings.rb
195 lines (162 loc) · 4.87 KB
/
about_strings.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
class AboutStrings < EdgeCase::Koan
def test_double_quoted_strings_are_strings
string = "Hello, World"
assert_equal __, string.is_a?(String)
end
def test_single_quoted_strings_are_also_strings
string = 'Goodbye, World'
assert_equal __, string.is_a?(String)
end
def test_use_single_quotes_to_create_string_with_double_quotes
string = 'He said, "Go Away."'
assert_equal __, string
end
def test_use_double_quotes_to_create_strings_with_single_quotes
string = "Don't"
assert_equal __, string
end
def test_use_backslash_for_those_hard_cases
a = "He said, \"Don't\""
b = 'He said, "Don\'t"'
assert_equal __, a == b
end
def test_use_flexible_quoting_to_handle_really_hard_cases
a = %(flexible quotes can handle both ' and " characters)
b = %!flexible quotes can handle both ' and " characters!
c = %{flexible quotes can handle both ' and " characters}
assert_equal __, a == b
assert_equal __, a == c
end
def test_flexible_quotes_can_handle_multiple_lines
long_string = %{
It was the best of times,
It was the worst of times.
}
assert_equal __, long_string.length
assert_equal __, long_string.lines.count
end
def test_here_documents_can_also_handle_multiple_lines
long_string = <<EOS
It was the best of times,
It was the worst of times.
EOS
assert_equal __, long_string.length
assert_equal __, long_string.lines.count
end
def test_plus_will_concatenate_two_strings
string = "Hello, " + "World"
assert_equal __, string
end
def test_plus_concatenation_will_leave_the_original_strings_unmodified
hi = "Hello, "
there = "World"
string = hi + there
assert_equal __, hi
assert_equal __, there
end
def test_plus_equals_will_concatenate_to_the_end_of_a_string
hi = "Hello, "
there = "World"
hi += there
assert_equal __, hi
end
def test_plus_equals_also_will_leave_the_original_string_unmodified
original_string = "Hello, "
hi = original_string
there = "World"
hi += there
assert_equal __, original_string
end
def test_the_shovel_operator_will_also_append_content_to_a_string
hi = "Hello, "
there = "World"
hi << there
assert_equal __, hi
assert_equal __, there
end
def test_the_shovel_operator_modifies_the_original_string
original_string = "Hello, "
hi = original_string
there = "World"
hi << there
assert_equal __, original_string
# THINK ABOUT IT:
#
# Ruby programmers tend to favor the shovel operator (<<) over the
# plus equals operator (+=) when building up strings. Why?
end
def test_double_quoted_string_interpret_escape_characters
string = "\n"
assert_equal __, string.size
end
def test_single_quoted_string_do_not_interpret_escape_characters
string = '\n'
assert_equal __, string.size
end
def test_single_quotes_sometimes_interpret_escape_characters
string = '\\\''
assert_equal __, string.size
assert_equal __, string
end
def test_double_quoted_strings_interpolate_variables
value = 123
string = "The value is #{value}"
assert_equal __, string
end
def test_single_quoted_strings_do_not_interpolate
value = 123
string = 'The value is #{value}'
assert_equal __, string
end
def test_any_ruby_expression_may_be_interpolated
string = "The square root of 5 is #{Math.sqrt(5)}"
assert_equal __, string
end
def test_you_can_get_a_substring_from_a_string
string = "Bacon, lettuce and tomato"
assert_equal __, string[7,3]
assert_equal __, string[7..9]
end
def test_you_can_get_a_single_character_from_a_string
string = "Bacon, lettuce and tomato"
assert_equal __, string[1]
# Surprised?
end
in_ruby_version("1.8") do
def test_in_ruby_1_8_single_characters_are_represented_by_integers
assert_equal __, ?a
assert_equal __, ?a == 97
assert_equal __, ?b == (?a + 1)
end
end
in_ruby_version("1.9") do
def test_in_ruby_1_9_single_characters_are_represented_by_strings
assert_equal __, ?a
assert_equal __, ?a == 97
end
end
def test_strings_can_be_split
string = "Sausage Egg Cheese"
words = string.split
assert_equal [__, __, __], words
end
def test_strings_can_be_split_with_different_patterns
string = "the:rain:in:spain"
words = string.split(/:/)
assert_equal [__, __, __, __], words
# NOTE: Patterns are formed from Regular Expressions. Ruby has a
# very powerful Regular Expression library. We will become
# enlightened about them soon.
end
def test_strings_can_be_joined
words = ["Now", "is", "the", "time"]
assert_equal __, words.join(" ")
end
def test_strings_are_unique_objects
a = "a string"
b = "a string"
assert_equal __, a == b
assert_equal __, a.object_id == b.object_id
end
end