Hello --

On Wed, 6 Dec 2000, Yuichi Masuda wrote:

> This code below seems to work.
> 
> 
> def get_depths (string)
>   dep_ar = string.collect do |line|
>     line.index /\S/
>   end
>   string.each do |line|
>     print dep_ar.uniq.sort.index(line.index /\S/) + 1, " #{line}"
>   end
> end

You could even do this:

def get_depths (string)
  string.each do |line|
    print string.collect {|ln|
	 ln.index(/\S/)}.uniq.sort.index(line.index(/\S/)) + 1, " #{line}"
  end
end

but the problem (with both, especially the second) is that you're
creating the array(s) on every iteration.  (Unless Ruby optimizes that
away -- but it's risky to count on that).

By the way, the parentheses around /\S/ eliminate a warning, at least
with 1.6.1.

Meanwhile, I realize I can do my version without the dreaded mapping
of (0...size):

      text.each_index do |i|
        yield depths[indents[i]],i
      end
    end

but I still wonder about "map_with_index" (or map_with_indices).


David

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