Question about Array.new(ary)

D

Daniel Berger

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
 
R

Robert Klemme

Daniel Berger said:
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
 
D

Daniel Berger

Robert said:
Daniel Berger said:
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.
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
 
T

ts

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%
 
D

Daniel Berger

ts said:
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%

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
 
L

linus sellberg

Daniel said:
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().
 
D

Daniel Berger

linus said:
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
 
T

ts

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%
 
D

Daniel Berger

ts said:
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%

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top