From: Michael Neumann Date: 2001-06-04T07:47:35+09:00 Subject: [ruby-talk:16191] Re: RCR: treatment of ranges within array definitions Phil Tomson wrote: > > > [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. This currently works in Ruby: array = [*1..3] # => [1, 2, 3] array = [*(1..3)] # => [1, 2, 3] array = [0, *(1..3)] # TypeError, failed to convert Range into Array ???? array = [0, *(1..3).to_a] # => [0,1,2,3] But after the * may not be any further parameter, so the following gives a parse error: array = [*(0..2).to_a, 3] I don't know why the TypeError is raised? Can someone tell me? The '*' normally calls #to_a, isn't it, and #to_a is defined by class Range. If we would allow '*' to be everywhere inside a parameter list, this would solve your problem, so you could write [*0..4,8,*10..12]. But I don't know if this would be possible, because the parser had to be changed. Regards, Michael -- Michael Neumann merlin.zwo InfoDesign GmbH http://www.merlin-zwo.de