From: Robert Klemme Date: 2004-01-15T23:31:41+09:00 Subject: Re: Concatenating variable number of Arrays ? "Florian Frank" schrieb im Newsbeitrag news:1074168191.4130.26.camel@lambda.eavesdrop.ping.de... > On Wed, 2004-01-14 at 16:33, Johan Holmberg wrote: > > The best way I have found until now, of doing what I want is > > > > arr2 = arr1.inject([]) {|acc,x| acc.concat x} > > You can also use "+" here: > > arr2 = arr1.inject([]) { |a,x| a + x } > > > But I feel that the thing I want to do (concatenating a variable > > number arrays) is such a natural thing, that it deserves a cleaner > > way of being expressed. > > If you extend Symbol like this: > > class Symbol > def to_proc > lambda { |a, b| a.__send__(self, b) } > end > end > > You could also write > > arr2 = arr1.inject([], &:+) > > That's pretty similar to how it is done in most functional programming > languages. Amazing! Only that it's less efficient since your approach creates more temporary arrays. Regards robert