# Question about Array's size instance method

**URL:** <https://www.ruby-forum.com/t/question-about-arrays-size-instance-method/81801>\
**Category:** Ruby\
**Created:** [January 14, 2007, 3:00pm UTC](https://www.ruby-forum.com/t/question-about-arrays-size-instance-method/81801 "2007-01-14T15:00:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![javamllama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@javamllama](https://www.ruby-forum.com/u/javamllama)\
**Post date:** [January 14, 2007, 3:00pm UTC](https://www.ruby-forum.com/t/question-about-arrays-size-instance-method/81801/1 "2007-01-14T15:00:56Z")

</div>

Hello all,

I have a quick question regarding the way the size instance method works  
for Arrays.

When an array is asked for its size multiple times in a piece of code  
(say a method). Which of the following happens?

a) The size is calculated every time the array is asked for it.  
b) The size is assigned to an instance variable the first time it is  
calculated and then returned when the array is subsequently asked for  
its size.

Any reply to this question would be gladly received.

Thanks in advance.

Matt.

---

<div class="post-metadata">

**Author:** ![javamllama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@javamllama](https://www.ruby-forum.com/u/javamllama)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/question-about-arrays-size-instance-method/81801/2 "2007-01-19T16:30:46Z")

</div>

Matt G. wrote:

> calculated and then returned when the array is subsequently asked for  
> its size.
> 
> Any reply to this question would be gladly received.
> 
> Thanks in advance.
> 
> Matt.

Ruby always knows the current size of the array. It doesn’t have to be  
calculated on demand.

---

<div class="post-metadata">

**Author:** ![javamllama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@javamllama](https://www.ruby-forum.com/u/javamllama)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/question-about-arrays-size-instance-method/81801/3 "2007-01-19T16:31:20Z")

</div>

Hi –

On Sun, 14 Jan 2007, Timothy H. wrote:

> > b) The size is assigned to an instance variable the first time it is  
> > Ruby always knows the current size of the array. It doesn’t have to be  
> > calculated on demand.

I’ll just add, in case it helps with future understanding of something  
similar, that I don’t think Ruby ever initializes an instance variable  
for you, without your specifically doing it (or implicitly, in the  
case of attr\_\*-generated methods).

David

---

<div class="post-metadata">

**Author:** ![javamllama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@javamllama](https://www.ruby-forum.com/u/javamllama)\
**Post date:** [September 25, 2007, 10:33pm UTC](https://www.ruby-forum.com/t/question-about-arrays-size-instance-method/81801/4 "2007-09-25T22:33:40Z")

</div>

On 1/15/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > case of attr\_\*-generated methods).
> > 
> > Call me pedantic, but I think it is important to point out that Dave’s  
> > comment excludes Ruby’s default initialization to nil for instance  
> > variables.
> 
> I won’t call you pedantic, if you don’t call me Dave 🙂

How much did you pay him for serving this one? 🙂

Robert

---

<div class="post-metadata">

**Author:** ![javamllama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@javamllama](https://www.ruby-forum.com/u/javamllama)\
**Post date:** [September 25, 2007, 10:45pm UTC](https://www.ruby-forum.com/t/question-about-arrays-size-instance-method/81801/5 "2007-09-25T22:45:55Z")

</div>

On Jan 14, 2007, at 9:23 AM, removed\_email\_address@domain.invalid wrote:

> I’ll just add, in case it helps with future understanding of something  
> similar, that I don’t think Ruby ever initializes an instance variable  
> for you, without your specifically doing it (or implicitly, in the  
> case of attr\_\*-generated methods).

Call me pedantic, but I think it is important to point out that Dave’s  
comment excludes Ruby’s default initialization to nil for instance  
variables.

p Object.new.instance\_variable\_get(’@foo’) # =\> nil

When learning Ruby, it took me a little while before I absorbed the  
fact that  
nil behaves as an object as opposed to a special value (like C’s NULL  
pointer)  
indicating the lack of a referenced object.

Gary W.

---

<div class="post-metadata">

**Author:** ![javamllama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@javamllama](https://www.ruby-forum.com/u/javamllama)\
**Post date:** [September 25, 2007, 10:50pm UTC](https://www.ruby-forum.com/t/question-about-arrays-size-instance-method/81801/6 "2007-09-25T22:50:44Z")

</div>

Hi –

On Mon, 15 Jan 2007, removed\_email\_address@domain.invalid wrote:

> On Jan 14, 2007, at 9:23 AM, removed\_email\_address@domain.invalid wrote:
> 
> > I’ll just add, in case it helps with future understanding of something  
> > similar, that I don’t think Ruby ever initializes an instance variable  
> > for you, without your specifically doing it (or implicitly, in the  
> > case of attr\_\*-generated methods).
> 
> Call me pedantic, but I think it is important to point out that Dave’s  
> comment excludes Ruby’s default initialization to nil for instance variables.

I won’t call you pedantic, if you don’t call me Dave 🙂

I agree; more accurate would be say: Instance variables default to  
nil, and Ruby never sets an instance variable for you.

David
