Skip to content

Commit c812a61

Browse files
committed
No more yield
1 parent 72598a1 commit c812a61

File tree

2 files changed

+24
-37
lines changed

2 files changed

+24
-37
lines changed

lib/traverse.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,23 @@ def initialize(data, left, right)
1111

1212
class Traverse
1313

14+
def self.capture(data)
15+
puts "Captured: #{data}"
16+
end
17+
1418
def self.root_only(tree)
15-
yield tree.data
19+
capture(tree.data)
1620
end
1721

18-
def self.with_preorder(node, &block)
22+
def self.with_preorder(node)
1923
# TODO
2024
end
2125

22-
def self.with_inorder(node, &block)
26+
def self.with_inorder(node)
2327
# TODO
2428
end
2529

26-
def self.with_postorder(node, &block)
30+
def self.with_postorder(node)
2731
# TODO
2832
end
2933

spec/traverse_spec.rb

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,51 +21,34 @@
2121
)
2222
end
2323

24+
let(:captured_results) { [] }
25+
26+
before do
27+
expect(Traverse).to receive(:capture).at_least(:once) {|data| captured_results.push(data) }
28+
end
29+
2430
it "visits the root node" do
25-
root_data = nil
26-
Traverse.root_only(sample_tree) do |data|
27-
root_data = data
28-
end
29-
expect(root_data).to eq 'F'
31+
Traverse.root_only(sample_tree)
32+
expect(captured_results).to eq ['F']
3033
end
3134

3235
it "traverses with pre-order" do
33-
visits = []
34-
Traverse.with_preorder(sample_tree) do |data|
35-
visits << data
36-
end
37-
38-
expect(visits.count).to eq 9
39-
expect(visits).to eq %w{F B A D C E G I H}
36+
Traverse.with_preorder(sample_tree)
37+
expect(captured_results).to eq %w{F B A D C E G I H}
4038
end
4139

4240
it "traverses with in-order" do
43-
visits = []
44-
Traverse.with_inorder(sample_tree) do |data|
45-
visits << data
46-
end
47-
48-
expect(visits.count).to eq 9
49-
expect(visits).to eq %w{A B C D E F G H I}
41+
Traverse.with_inorder(sample_tree)
42+
expect(captured_results).to eq %w{A B C D E F G H I}
5043
end
5144

5245
it "traverses with post-order" do
53-
visits = []
54-
Traverse.with_postorder(sample_tree) do |data|
55-
visits << data
56-
end
57-
58-
expect(visits.count).to eq 9
59-
expect(visits).to eq %w{A C E D B H I G F}
46+
Traverse.with_postorder(sample_tree)
47+
expect(captured_results).to eq %w{A C E D B H I G F}
6048
end
6149

6250
it "traverses with level-order", :pending => "Extension!" do
63-
visits = []
64-
Traverse.with_levelorder(sample_tree) do |data|
65-
visits << data
66-
end
67-
68-
expect(visits.count).to eq 9
69-
expect(visits).to eq %w{F B G A D I C E H}
51+
Traverse.with_levelorder(sample_tree)
52+
expect(captured_results).to eq %w{F B G A D I C E H}
7053
end
7154
end

0 commit comments

Comments
 (0)