From: Phil Tomson Date: 2001-06-04T06:40:15+09:00 Subject: [ruby-talk:16189] RCR: treatment of ranges within array definitions [I sent this yesterday via the newsgroup comp.lang.ruby, but I don't think it ever went anywhere, so now I'm sending to the mailing list - sorry if it's a duplication] I've been converting a lot of perl code to Ruby lately. In perl if I put a range into an array that range is expanded so that the array contains each element described by the range (Perl doesn't have a range type). So in perl if I do: (0..4,8,10..12) I get an array that contains: (0,1,2,3,4,8,10,11,12) If I declare an array in Ruby this way: array = [0..4,8,10..12] array contains three elements, the first and third elements are of range type. In Ruby in order to get the same array that I declared in Perl above, I have to do: array = (0..4).to_a+[8]+(10..12).to_a which I'm sure most will agree is a bit cumbersome... So, here is my poposal: When a range appears within an array definition (between '[' and ']') expand the range into an array containing each of the elements defined by the range unless the range is defined within parenthesis. For example: array = [0..4,8,10..12] array would contain: 0,1,2,3,4,8,10,11,12 array2 = [(0..4),8,10..12] array2 would contain: (0..4),8,10,11,12 where the first element is a range type. I realize that this could break some existing code, someone might have some current code with [0..4,3..5,20..24] where they expect an array with three ranges. They would have to change it to: [(0..4),(3..5),(20..24)] in order for it to work that way if this RCR were implemented. Comments? Phil