From: Christopher Dicely Date: 2008-02-07T11:16:46+09:00 Subject: Re: Array Practice Oh, and uses []. On Feb 6, 2008 6:15 PM, Christopher Dicely wrote: > Anytime I want to "build up" something from an existing Enumerable > object (Array, etc.) in Ruby, I usually look to inject or collect; the > most elegant of the way I've come up with of doing exactly what you > want uses ==, *, uniq, collect, find_all, and size, and the function > body fits on a single line without semi-colons. (I'm not holding up > that it is a one-liner as a strength, just as a description.) > > On Feb 6, 2008 3:20 PM, Adam Akhtar wrote: > > > As some of you may know from previous threads im trying to practice > > specific areas of ruby. One simple exercise i set myself was taking a > > elements in an array such as > > > > [1,2,3,1,2,2,4,3,2,1] > > > > and then grouping them together and putting them in subarrays within an > > array like this > > > > [ [1,1,1], [2,2,2,2] , [3,3] ,[4] ] > > > > Heres how i did it (im new to both programming and ruby) > > > > def subpack(list) > > uniqlist = Array.new > > subpacklist = Array.new > > list.sort! > > > > uniqlist = list.uniq > > uniqlist.each_index do |i| > > rangeleft = list.index(uniqlist[i]) > > rangeright = list.rindex(uniqlist[i]) > > subpacklist << list.slice(rangeleft..rangeright) > > end > > subpacklist > > end > > > > > > WITHOUT GIVING ME YOUR SOLUTION IN FULL, how could i improve this... > > i.e. give me some hints, the names of some methods but not the solution > > as i want a go myself. > > -- > > Posted via http://www.ruby-forum.com/. > > > > >