[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Question about Array.new(ary

djberg96

2/23/2005 4:31:00 PM

Hi all,

I see that Ruby 1.8 added the option of passing an array to Array.new.

a = Array.new([1,2,3])
p a # [1,2,3]

My question is, what is the point of this construct? If it's just a
dup, shouldn't we just use .dup? What advantage am I missing here?

I ask in part because I'm doing a little refactoring project, and
having to support this sort of pseudo overloading is a pain on the C
side.

Regards,

Dan

8 Answers

Robert Klemme

2/23/2005 5:02:00 PM

0


"Daniel Berger" <djberg96@hotmail.com> schrieb im Newsbeitrag
news:1109176271.986202.113420@z14g2000cwz.googlegroups.com...
> Hi all,
>
> I see that Ruby 1.8 added the option of passing an array to Array.new.
>
> a = Array.new([1,2,3])
> p a # [1,2,3]
>
> My question is, what is the point of this construct? If it's just a
> dup, shouldn't we just use .dup? What advantage am I missing here?

Someone might want to make sure that he gets an array. Using #dup you get
whatever class the instance has (could be a sub class of Array).

> I ask in part because I'm doing a little refactoring project, and
> having to support this sort of pseudo overloading is a pain on the C
> side.

But this is not the only place, is it? I mean, you will have to deal with
this in *all* places in a general way. Pity is, you can't derive types
from the signature...

Regards

robert

djberg96

2/23/2005 6:00:00 PM

0

Robert Klemme wrote:
> "Daniel Berger" <djberg96@hotmail.com> schrieb im Newsbeitrag
> news:1109176271.986202.113420@z14g2000cwz.googlegroups.com...
> > Hi all,
> >
> > I see that Ruby 1.8 added the option of passing an array to
Array.new.
> >
> > a = Array.new([1,2,3])
> > p a # [1,2,3]
> >
> > My question is, what is the point of this construct? If it's just
a
> > dup, shouldn't we just use .dup? What advantage am I missing here?
>
> Someone might want to make sure that he gets an array. Using #dup
you get
> whatever class the instance has (could be a sub class of Array).

Huh? In fact, turning a subclass of an Array back into an Array is
about the only use, since it's mandatory that the argument be an Array
to begin with. That strikes me as some sort of useless typecasting and
has absolutely no place in Ruby.

> > I ask in part because I'm doing a little refactoring project, and
> > having to support this sort of pseudo overloading is a pain on the
C
> > side.
>
> But this is not the only place, is it? I mean, you will have to deal
with
> this in *all* places in a general way. Pity is, you can't derive
types
> from the signature...

No, I'm going to eliminate what I consider to be garbage. If I had
been paying closer attention to the dev list and change log I would
have screamed sooner.

In general I'm getting tired of pet functions and signatures being
added to core classes, especially the "let's add an optional block to
method X so I can avoid a call to .map" stuff.

More later.

Dan

ts

2/23/2005 6:31:00 PM

0

>>>>> "D" == Daniel Berger <djberg96@hotmail.com> writes:

D> Huh? In fact, turning a subclass of an Array back into an Array is
D> about the only use, since it's mandatory that the argument be an Array
D> to begin with.

uln% ruby -e 'class A; def to_ary() [1, 2] end; end; p Array.new(A.new)'
[1, 2]
uln%


--

Guy Decoux

djberg96

2/23/2005 6:55:00 PM

0

ts wrote:
> >>>>> "D" == Daniel Berger <djberg96@hotmail.com> writes:
>
> D> Huh? In fact, turning a subclass of an Array back into an Array
is
> D> about the only use, since it's mandatory that the argument be an
Array
> D> to begin with.
>
> uln% ruby -e 'class A; def to_ary() [1, 2] end; end; p
Array.new(A.new)'
> [1, 2]
> uln%
>
>
> --
>
> Guy Decoux

An interesting if obscure use. I suspect most people are going to be
more direct and define a to_a method for their class, rather than the
roundabout approach used here. It still feels like a typecast.

In any case this is a slippery slope I don't think Ruby should have
ever started down.

Regards,

Dan

linus sellberg

2/23/2005 10:17:00 PM

0

Daniel Berger wrote:
> Huh? In fact, turning a subclass of an Array back into an Array is
> about the only use, since it's mandatory that the argument be an Array
> to begin with. That strikes me as some sort of useless typecasting and

It is not correct that the argument must be an array.
ri tells me that

Array.new(size=0, obj=nil)
Array.new(array)
Array.new(size) {|index| block }

are the different ways to call Array.new().

djberg96

2/24/2005 1:36:00 AM

0

linus sellberg wrote:
> Daniel Berger wrote:
> > Huh? In fact, turning a subclass of an Array back into an Array is
> > about the only use, since it's mandatory that the argument be an
Array
> > to begin with. That strikes me as some sort of useless typecasting
and
>
> It is not correct that the argument must be an array.
> ri tells me that
>
> Array.new(size=0, obj=nil)
> Array.new(array)
> Array.new(size) {|index| block }
>
> are the different ways to call Array.new().

Correct. What I meant to say is that if it's not a Fixnum, it must be
an Array. Sorry for the confusion.

Regards,

Dan

ts

2/24/2005 8:44:00 AM

0

>>>>> "D" == Daniel Berger <djberg96@hotmail.com> writes:

D> In any case this is a slippery slope I don't think Ruby should have
D> ever started down.

I hope that you know this ?

uln% ruby -e 'class A; def to_int() 12 end; end; p Array.new(A.new)'
[nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
uln%


--

Guy Decoux

djberg96

2/24/2005 2:47:00 PM

0

ts wrote:
> >>>>> "D" == Daniel Berger <djberg96@hotmail.com> writes:
>
> D> In any case this is a slippery slope I don't think Ruby should
have
> D> ever started down.
>
> I hope that you know this ?
>
> uln% ruby -e 'class A; def to_int() 12 end; end; p Array.new(A.new)'
> [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
> uln%
>
>
> --
>
> Guy Decoux

I guess you're merely pointing out what Linus Sellberg mentioned. Yes,
I'm aware that if the first argument is an integer, the behavior is
different.

I'm only talking about the behavior of Array.new where the first
argument is an Array. It should be removed IMHO. It should strictly
be:

Array.new(size,obj=nil)
Array.new(size,obj=nil){ ... }

Mind you, I wouldn't mind seeing the block form removed, but I think
I'm outvoted there.

Regards,

Dan