From: Charles Comstock Date: 2004-06-12T13:43:39+09:00 Subject: Re: Array#rest On Sat, 12 Jun 2004, Florian Frank wrote: > Hi, > > On 11.06.2004, at 18:23, Charles Comstock wrote: > > I don't want a destructive function. A number of times I have used > > the shift > > and the remainder, but there are a number of times you want a non > > destructive > > copy of the rest of the list. Hence rest ;p > > If you don't want to destroy the original list you can use the splat > operator > in an assignment: > > a = [ 1, 2, 3, 4 ] > # => [1, 2, 3, 4] > first, *rest = a > # => [1, 2, 3, 4] > first > # => 1 > rest > # => [2, 3, 4] > Yea I know I can do it like that, but sometimes I don't want the extra variable running around. The most recent case I wanted a rest function was a situation where I had an array in which the first arg was used to select the command object and then the rest of the array was used as the parameter list to the command's instanciation. I wanted to keep the original input as a single array so that I could print it out for debug output later if I needed to, keeping the command name so it was clearer. Obviously I could seperate it into command name and params and keep around the original input, but I really only need each of the parts once, so it seemed logical to just grab out the parts I needed when I passed them as parameters. It's not an issue of not knowing how to split them, or even that the method isn't already available through slice(1..-1), I just thought that array rest would be more readable in this case. It's not something really frequent but I find myself using stuff like this here and there, and it just seemed like a useful function to have. It just seemed to make sense what with ruby already having a first function. On a side note, I had to double check your above example. I knew you could split like that, but I didn't realize that it performed a copy and was not referencing the original parts of a. I suppose it is more useful that way, and am glad it is, but my gut reaction to the symantics of that was that it performed like ='s generally does, acting as an alias instead. You learn something every day I guess. Charles Comstock