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.
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.
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.
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
···
On Jan 14, 2007, at 9:23 AM, dblack@wobblini.net 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).
On Jan 14, 2007, at 9:23 AM, dblack@wobblini.net 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.
On 1/15/07, dblack@wobblini.net <dblack@wobblini.net> wrote:
Hi --
On Mon, 15 Jan 2007, gwtmp01@mac.com wrote:
>
> On Jan 14, 2007, at 9:23 AM, dblack@wobblini.net 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
--
"The best way to predict the future is to invent it."
- Alan Kay