Question about Array's size instance method

M

Matt Gretton

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.
 
T

Timothy Hunter

Matt said:
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.
Ruby always knows the current size of the array. It doesn't have to be
calculated on demand.
 
D

dblack

Hi --

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

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
G

gwtmp01

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

dblack

Hi --

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

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top