From: "David A. Black" Date: 2009-10-02T00:20:32+09:00 Subject: Re: how do you do this --1926193751-2038169463-1254410416=:15385 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-2038169463-1254410416=:15385" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --1926193751-2038169463-1254410416=:15385 Content-Type: TEXT/PLAIN; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8BIT Hi -- On Thu, 1 Oct 2009, Jes�s Gabriel y Gal�n wrote: > On Thu, Oct 1, 2009 at 2:23 PM, George George wrote: >> Given an array of strings e.g. >> �x = ["abc","abcde" "def","xyzwj"] and of different lengths, >> how can you efficiently create new arrays of strings which are of the >> same length. for example the above array can be transformed into >> >> x1 = ["abc","def"] >> x2 = ["abcde","xyzwj"] > > You might want to look at group_by: > > %w{abc ads adfdf adfdw fefm mfekmw fmdms}.group_by {|x| x.length} It's interesting how often the need for group_by without the keys comes up. Meaning, in this case, to get the new arrays you'd ultimately do: arr.group_by(&:length).values and I believe there was at least one similar case mentioned here recently. I wonder whether it would be cool to have a method that did this -- in effect: module Enumerable def group_by_without_keys(&block) group_by(&block).values end end I'm not sure what it should be called, though. David -- David A. Black, Director Ruby Power and Light, LLC (http://www.rubypal.com) Ruby/Rails training, consulting, mentoring, code review Book: The Well-Grounded Rubyist (http://www.manning.com/black2) --1926193751-2038169463-1254410416=:15385-- --1926193751-2038169463-1254410416=:15385--