Not supposed behaviour of Array.new(2,[])

Hi,

a=[,]
[, ]
a[0].push(1)
[1]
a
[[1], ]

Works great, isn’t it ? :slight_smile:
Now, let’s do “the same” with Array.new method:

a=Array.new(2,)
[, ]
a[0].push(1)
[1]
a
[[1], [1]]

It isn’t strange for me (I know that kind of behaviour from python),
but, is it documented anywhere ? I’m just searching for a point of
reference… ;]

···


gminick (at) underground.org.pl http://gminick.linuxsecurity.pl/
[ “Po prostu lubie poranna samotnosc, bo wtedy kawa smakuje najlepiej.” ]

Hi –

···

On Mon, 25 Nov 2002, gminick wrote:

Hi,

a=[,]
[, ]
a[0].push(1)
[1]
a
[[1], ]

Works great, isn’t it ? :slight_smile:
Now, let’s do “the same” with Array.new method:

a=Array.new(2,)
[, ]
a[0].push(1)
[1]
a
[[1], [1]]

It isn’t strange for me (I know that kind of behaviour from python),
but, is it documented anywhere ? I’m just searching for a point of
reference… ;]

$ ri Array.new
------------------------------------------------------------- Array::new
Array.new( anInteger=0, anObject=nil ) → anArray

 Returns a new array, optionally with a size and initial value (that
 is, anInteger references to the same anObject).
    Array.new                #=> []
    Array.new(2)             #=> [nil, nil]
    Array.new(5, "A")        #=> ["A", "A", "A", "A", "A"]
    Array.new(2, Hash.new)   #=> [{}, {}]

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hi,

a=[,]
[, ]
a[0].push(1)
[1]
a
[[1], ]

Works great, isn’t it ? :slight_smile:
Now, let’s do “the same” with Array.new method:

a=Array.new(2,)

You create an array holding 2 references to the same (empty) array.
In fact you’re doing
tmp =
a = Array.new(2,tmp) # a = [tmp, tmp]

Having later a[0] == a[1] shouldn’t surprise you.

[, ]

a[0].push(1)
[1]
a
[[1], [1]]

It isn’t strange for me (I know that kind of behaviour from python),
but, is it documented anywhere ? I’m just searching for a point of
reference… ;]

http://www.glue.umd.edu/~billtj/ruby.html#default

···

On Mon, Nov 25, 2002 at 07:42:19AM +0900, gminick wrote:


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

“Who is General Failure and why is he reading my hard disk?”
Microsoft spel chekar vor sail, worgs grate !!
– Felix von Leitner, leitner@inf.fu-berlin.de

Yeah. I always wished there was a nice way to do that, like with blocks.
For example:

a=Array.new(2) { }
[, ]
a[0].push(1)
[1]
a
[[1], ]

So Array.new yields the block twice, the return values of which are the
elements in the array. I guess we could pass in the index into the block,
too, if we wanted. Setting up a triangular matrix (something I know each of
you wants to do on a daily basis) would be as cute as this:

Array.new(n) {|i| Array.new(n-i)}

Anyway, it feels like a Ruby thing to me. Don’t you think so? I don’t know
how I would extend this myself, though. (My Ruby isn’t that fancy yet.)

Chris

···

----- Original Message -----
From: “gminick” gminick@underground.org.pl
Newsgroups: comp.lang.ruby
Sent: Sunday, November 24, 2002 2:42 PM
Subject: Not supposed behaviour of Array.new(2,)

Hi,

a=[,]
[, ]
a[0].push(1)
[1]
a
[[1], ]

Works great, isn’t it ? :slight_smile:
Now, let’s do “the same” with Array.new method:

a=Array.new(2,)
[, ]
a[0].push(1)
[1]
a
[[1], [1]]

It isn’t strange for me (I know that kind of behaviour from python),
but, is it documented anywhere ? I’m just searching for a point of
reference… ;]


gminick (at) underground.org.pl http://gminick.linuxsecurity.pl/
[ “Po prostu lubie poranna samotnosc, bo wtedy kawa smakuje najlepiej.” ]

Hi,

I think in 1.7.2 and later you can do just exactly like that. A wish come
true? :slight_smile:

Regards,

Bill

···

Chris nemo@hellotree.com wrote:

Yeah. I always wished there was a nice way to do that, like with blocks.
For example:

a=Array.new(2) { }
[, ]
a[0].push(1)
[1]
a
[[1], ]

Thanks :slight_smile:

···

On Mon, Nov 25, 2002 at 07:44:55AM +0900, dblack@candle.superlink.net wrote:

 Returns a new array, optionally with a size and initial value (that
 is, anInteger references to the same anObject).


gminick (at) underground.org.pl http://gminick.linuxsecurity.pl/
[ “Po prostu lubie poranna samotnosc, bo wtedy kawa smakuje najlepiej.” ]

I think in 1.7.2 and later you can do just exactly like that. A wish come
true? :slight_smile:

···

----- Original Message -----
From: “William Djaja Tjokroaminata” billtj@z.glue.umd.edu


It is! God, I love this language! (I really live in the pickaxe book;
what’s a better reference for the built-in classes and the standard
library?)

Chris

I can confirm in Windows 1.7.3-6, particularly for the “triangular”
array Chris suggested.

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.11.25 at 11.50.38

···

On Mon, 25 Nov 2002 14:43:02 +0900, William Djaja Tjokroaminata wrote:

Chris nemo@hellotree.com wrote:

Yeah. I always wished there was a nice way to do that, like with
blocks.
I think in 1.7.2 and later you can do just exactly like that. A
wish come true? :slight_smile:

Hi Chris,

I think it is similar to the difference between text books and journal
papers. Text books give you solid fundamental understanding, but they do
not cover the latest technologies. In Ruby, probably this discussion
group is one of the representatives of the Ruby “journal papers”. I also
knew that particular array feature from following this discussion group
and not from other references. On the other hand, if you know everything
in the pickaxe book, I think you are already a very solid rubyist.

Regards,

Bill

···

Chris nemo@hellotree.com wrote:

It is! God, I love this language! (I really live in the pickaxe book;
what’s a better reference for the built-in classes and the standard
library?)