From: "Jesús Gabriel y Galán" Date: 2007-11-22T23:03:17+09:00 Subject: Re: How to give depth to arrays? On Nov 22, 2007 10:28 AM, Chris Morales wrote: > Okay, I made it kinda works, clunky and ugly though... > > > class Array > def max > max = 0; > self.each{ |i| max = i>max ? i : max } > max > end > def min > min = 9999; > self.each{ |i| min = i min > end > def depthen > base_depth = self.min > range = [0,0] > in_flag, out_flag = false, false > self.each_index{ |i| > if self[i]>base_depth && !in_flag > in_flag = true; > range[0] = i > end > if self[i] <= base_depth && !out_flag > out_flag = true > range[1] = i-1 > end > } > to_insert = self.slice!(eval(range.join('..'))) > to_insert.depthen if to_insert.max>to_insert.min > self.insert( range[0], to_insert ) > end > end > > p [2,3,3,5,4,4].depthen => [2,[3,3,[[5],4,4]]] > > I will be really thankful if anybody could help me improve this monster > with nice ruby tricks. With your code: [2,3,5,3].depthen # => [2, [3, [[5], 3]]] Shouldn't this be: [2, [3, [[5]], 3]] ? I'm trying to think of an elegant solution, but I'm kind of slow today... Jesus.