Hello --

On Wed, 6 Dec 2000, Fabio Mascarenhas wrote:


> def get_depths (string)
>   lens=get_lenghts(string)
>   depths=get_depths(string,lens)     # <----
>   print_depths(string,depths)
> end


Looks like infinite recusion to me :-)  (Except you'll
never get that far -- check the spelling in the previous
line.)

I'd go for a slightly less refactored version.  Of course,
it all depends what you think you might want to do on
other occasions with the methods you've created.  Anyway,
here's a different approach:

   def get_depths(text)
     depths = {}

     indents = text.map do |s|
       s.index /\S/
     end

     indents.uniq.sort.each_with_index do |n,x|
       depths[n] = x + 1
     end

     (0...text.size) .map do |i|
       yield (depths[indents[i]],i)
     end
   end

   def print_depths(text)
     text = text.to_a
     get_depths(text) do |d,i|
       print "#{d} #{text[i]}"
     end
   end


Question arising from this: Is there a better way to do, in effect,
"map_with_index" than iterating from 0 to size - 1?  


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav