Hello --

On Wed, 6 Dec 2000, Joseph McDonald wrote:

> 
> Wondering if someone could critique this code and give me the
> "ruby way" of doing it.  Basically, what it does it get the
> "level" of different items in a menu based on the amount
> of whitespace before the item.   The code does function
> properly, but I have a feeling I may be attacking this the
> wrong way.

I dare not speak for Ruby way-hood, but here's one thing:


>   string.each do |line|
>     len = line =~ /^(\s+)/ ? $1.length : 0
>     lens[i] = len
>     i+=1
>   end

You could do this:

  string.each_with_index do |line,i|
    lens[i] = /^(\s+)/.match(line) ?  $1.length : 0
  end

(fun OO regex stuff :-)
and not have to do the explicit incrementing of i.  Ditto on
the other string.each iteration a little later.


David

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