How about this?
I don't know wheter this is in Ruby way, but
I think it's a bit simpler.
(I'm afraid this will eat more memory, though.)
def insert(ar,n)
return ar + [n] if ar[-1] < n
sentinel = false
ar.collect{|e|
if sentinel
e
else
if e <= n
sentinel = true if n == e
e
else
sentinel = true
[n,e]
end
end
}.flatten
end
def get_depths (string)
dep_ar = []; dep_ar[0] = 0
string.each do |line|
dep_ar = insert(dep_ar,(line =~ /^(\s+)/ ? $1.length : 0))
end
string.each do |line|
print dep_ar.index(line =~ /^(\s+)/ ? $1.length : 0) + 1, " #{line}"
end
end
get_depths(<<EOF
text1@url1
text2@url2
text3@url3
text3.1 / url3.1
text3.2 / url3.2
text4@url4
text5@url5
text6@url6
text7@url7
text8@url8
text9@url9
EOF
)