Adding yet another Array.new form

From: Nikolai Weibull
[mailto:mailing-lists.ruby-talk@rawuncut.elitemail.org]
Sent: Sunday, August 07, 2005 5:43 AM
To: ruby-talk ML
Subject: Adding yet another Array.new form

How about adding a fifth way of constructing new arrays?:

Array.new{|array| block }

Conflicts with the current 4th form, unless you want a "no argument but
block exists" form, e.g. Array.new(x){ .. } would be different from
Array.new{ .. }.

The Array.new source is already a mish-mash of if-else statements.
Adding this would just add another layer of goo to already ugly source
code.

If anything, we should remove the 3rd form altogether, Array.new(ary),
and replace the 4th form with what (I think) has been suggested in later
replies to this thread, i.e. yield self if block_given? for most, if not
all, constructors.

Here, array is the newly created array, and the block will
initialize it as it sees fit, e.g,

ary = Array.new{ |ary| 5.times{ |i| ary << i } },

Just to clarify, what should "ary" look like at the end of this example?

Regards,

Dan

···

-----Original Message-----

Berger, Daniel wrote:

> How about adding a fifth way of constructing new arrays?:
>
> Array.new{|array| block }

Conflicts with the current 4th form, unless you want a "no argument
but block exists" form, e.g. Array.new(x){ .. } would be different
from Array.new{ .. }.

Yes, that was the idea.

If anything, we should remove the 3rd form altogether, Array.new(ary),
and replace the 4th form with what (I think) has been suggested in
later replies to this thread, i.e. yield self if block_given? for
most, if not all, constructors.

Well, how do you propose to do it for most, but not all constructors?
Yes, I think that there may be merit to adding a Class.create method as
well, but that's a lot more drastic than adding a simple case for
Array.new, which already has a lot of ways of creating itself. One more
wouldn't be imposing much, but would be rather useful I feel (more
useful with the 4th form in my opinion).

> Here, array is the newly created array, and the block will
> initialize it as it sees fit, e.g,
>
> ary = Array.new{ |ary| 5.times{ |i| ary << i } },

Just to clarify, what should "ary" look like at the end of this
example?

ary # => [0, 1, 2, 3, 4],
        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);}