From: dblack@... Date: 2007-01-14T23:23:43+09:00 Subject: Re: Question about Array's size instance method Hi -- On Sun, 14 Jan 2007, Timothy Hunter wrote: > Matt Gretton wrote: >> 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. 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)