Robert Klemme wrote:

> They are methods in Enumerable.  map returns an array and inject can be
> used to return an array:
> 
> > > (1..10).map{|x| x + 0.5}
> => [1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5]
> > > (1..10).inject([]){|a,x| a << x + 0.5}
> => [1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5]
> 
> If you need to create an Array from something enumerable these are quite
> conveninent.

Yes, yes, but as you say, you still need the enumerable collection.

> > Sure, the format I'm suggesting is perhaps limited in applicability,
> > but how often do you do Array.new(size){ |index| ... }?  I have four
> > methods in one class whose bodies are a big returning, like
> > parse_modifiers
> > above, that sets up an array and then returns it,

> In this case I'd prefer the general approach that has been suggested
> already:
> 
> > > class Class
> > >   def create(*a)
> > >     obj = new(*a)
> > >     yield obj
> > >     obj
> > >   end
> > > end
> => nil
> > > Array.create do |ar|
> ?>   ar << 1 << 2
> > > end
> => [1, 2]

Again, this is a little more drastic than adding a line or two to the
initializer of Array for handling a case where the size argument is nil.
Sure, I think that a create method would have its uses, but that doesn't
mean that we can't have a slightly more flexible Array.new as well,
        nikolai

-- 
Nikolai Weibull: now available free of charge at http://bitwi.se/!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}