[][100000000] = 1 whee!

:slight_smile: Just curious as to why Ruby automatically increases an array's size when items are added. I could see where it would be convenient, but it also seems like bad practice. One can, and should use a hash as a sparse array.

Mike

:slight_smile: Just curious as to why Ruby automatically increases an array's size
when items are added. I could see where it would be convenient, but it
also seems like bad practice. One can, and should use a hash as a sparse
array.

Your answer is in your question. "One can, and should, use a hash as a
sparse array." If you want a sparse array, use a Hash. And Array is an
array, and acts like one.

Mike

--Greg

路路路

On Mon, Nov 17, 2008 at 09:56:39PM +0900, Mike Austin wrote:

Mike Austin wrote:

:slight_smile: Just curious as to why Ruby automatically increases an array's size when items are added. I could see where it would be convenient, but it also seems like bad practice. One can, and should use a hash as a sparse array.

Because it's an array, not a sparse array.

- Charlie

Charles Oliver Nutter wrote:

Mike Austin wrote:

:slight_smile: Just curious as to why Ruby automatically increases an array's size when items are added. I could see where it would be convenient, but it also seems like bad practice. One can, and should use a hash as a sparse array.

Because it's an array, not a sparse array.

- Charlie

True enough :slight_smile: But what about in terms of practicality? Does it make working with arrays that much simpler? I use Ruby Arrays as stacks mostly, where you don't run into setting values outside the current range.

~ Mike

Ah, a "sparse array". Is there value in this? I once re-implemented
Array using a Hash as the underlying store. It was just for fun and
now sits in my "trinkets" drawer. Should I release it?

T. Rinkets

路路路

On Nov 17, 10:39 am, Charles Oliver Nutter <charles.nut...@sun.com> wrote:

Mike Austin wrote:
> :slight_smile: Just curious as to why Ruby automatically increases an array's size
> when items are added. I could see where it would be convenient, but it
> also seems like bad practice. One can, and should use a hash as a
> sparse array.

Because it's an array, not a sparse array.

Mike Austin wrote:

Charles Oliver Nutter wrote:

Mike Austin wrote:

:slight_smile: Just curious as to why Ruby automatically increases an array's size when items are added. I could see where it would be convenient, but it also seems like bad practice. One can, and should use a hash as a sparse array.

Because it's an array, not a sparse array.

- Charlie

True enough :slight_smile: But what about in terms of practicality? Does it make working with arrays that much simpler? I use Ruby Arrays as stacks mostly, where you don't run into setting values outside the current range.

No, it probably doesn't do a lot for arrays in general, but I suppose it does allow you to use an array as an N-wide list without immediately allocating the full backing store. That probably has some value.

- Charlie

I use arrays for other purposes... however the best answer is probably to think of Ruby, or any other programming language for that matter, as a game with a fixed set of rules. You have to play by those rules... or play another game.

That leads to the answer that Ruby does this because the designer chose to do it that way. No other reason needed.

Ron

Mike Austin wrote:

路路路

Charles Oliver Nutter wrote:

Mike Austin wrote:

:slight_smile: Just curious as to why Ruby automatically increases an array's size when items are added. I could see where it would be convenient, but it also seems like bad practice. One can, and should use a hash as a sparse array.

Because it's an array, not a sparse array.

- Charlie

True enough :slight_smile: But what about in terms of practicality? Does it make working with arrays that much simpler? I use Ruby Arrays as stacks mostly, where you don't run into setting values outside the current range.

~ Mike

--
Ron Fox
NSCL
Michigan State University
East Lansing, MI 48824-1321

Ron Fox wrote:

I use arrays for other purposes... however the best answer is probably to think of Ruby, or any other programming language for that matter, as a game with a fixed set of rules. You have to play by those rules... or play another game.

That leads to the answer that Ruby does this because the designer chose to do it that way. No other reason needed.

Ron

I found the quote:

"The rule for ruby is that all decisions are made to make it more fun to program in."

I'd have to agree, not dealing with array sizing makes it more fun to use Ruby. If you're wondering why I keep pressing the subject, it's because I'm currently doing research for a new language.

Not to start a flame war, but the ability for Python to chain operators,
eg "-1 < i < 1", makes it "fun to use"... any thoughts? One drawback is that it's only syntactic sugar and breaks down when you try "(-1 < i) < 1".

Mike

路路路

Mike Austin wrote:

Charles Oliver Nutter wrote:

Mike Austin wrote:

:slight_smile: Just curious as to why Ruby automatically increases an array's size when items are added. I could see where it would be convenient, but it also seems like bad practice. One can, and should use a hash as a sparse array.

Because it's an array, not a sparse array.

- Charlie

True enough :slight_smile: But what about in terms of practicality? Does it make working with arrays that much simpler? I use Ruby Arrays as stacks mostly, where you don't run into setting values outside the current range.

~ Mike