-
Notifications
You must be signed in to change notification settings - Fork 0
/
asciitree.rb
47 lines (43 loc) · 920 Bytes
/
asciitree.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
###################################################
###
## File: asciitree.rb
## Desc: Saw a python version of asciitree when playing with Google's syntaxNet software
## Thought the python code was too complex for what it did; so wrote this.
#
def asciitree(an_array, level=0)
an_array.each do |a|
if Array == a.class
if Array == a.first.class
prefix = ("|-- "*level) + "|-- v"
(level+1).times { prefix.gsub!('|-- |', '| |') }
puts prefix
end
asciitree(a, level+1)
else
prefix = "|-- "*level
level.times { prefix.gsub!('|-- |', '| |') }
puts prefix + a.to_s
end
end
end
=begin
array = [0, [1, 1, 1], 0, [1, [2, [[4,4,4],3,3], 2], 1], 0]
asciitree array
should_ne = <<EOS
0
|-- 1
|-- 1
|-- 1
0
|-- 1
| |-- 2
| | |-- v
| | | |-- 4
| | | |-- 4
| | | |-- 4
| | |-- 3
| | |-- 3
| |-- 2
|-- 1
0
EOS