-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathrspec_homework.rb
70 lines (58 loc) · 1.34 KB
/
rspec_homework.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
# Question:
describe "#symbolize" do
it "creates an array of symbols" do
arr = %w|b c d c a|
expected = [:b, :c, :d, :c, :a]
expect(arr.symbolize).to eq expected
end
end
# Answer:
class Array
def symbolize
map(&:to_sym)
end
end
# 2.1 Question:
describe "#has_string?" do
it "returns true if self has a string" do
arr = [:bob, "the", 42]
expect(arr.has_string?).to eq true
end
it "returns false if self doesn't have a string" do
arr = [:shut, :em, :down]
expect(arr.has_string?).to eq false
end
end
# 2.2 Question:
describe "#sort_by_length" do
it "orders words from smallest to largest" do
arr = %w|this is the story of a girl|
expected = %w|a is of the this girl story|
expect(arr.sort_by_length).to eq expected
end
end
# 2.3 Question:
describe "#mean" do
it "calculates average" do
arr = [1, 2, 3, 4]
expect(arr.mean).to eq 2.5
end
end
# 2.4 Question:
describe "#unzip" do
it "deconstructs nested array" do
nums = [1, 2, 3]
lets = %w|a b c|
zipped = nums.zip(lets)
expected = [["a", "b", "c"], [1, 2, 3]]
expect(zipped.unzip).to eq expected
end
end
# 2.5 Question:
describe "#super_compact" do
it "removes nil and empty elements" do
arr = [:bob, "", nil, [], "joe"]
expected = [:bob, "joe"]
expect(arr.super_compact).to eq expected
end
end