Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Question about Array's size instance method

Reply
Thread Tools

Question about Array's size instance method

 
 
Matt Gretton
Guest
Posts: n/a
 
      01-14-2007
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.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Timothy Hunter
Guest
Posts: n/a
 
      01-14-2007
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.

 
Reply With Quote
 
 
 
 
dblack@wobblini.net
Guest
Posts: n/a
 
      01-14-2007
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)

 
Reply With Quote
 
gwtmp01@mac.com
Guest
Posts: n/a
 
      01-14-2007

On Jan 14, 2007, at 9:23 AM, 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 Wright




 
Reply With Quote
 
dblack@wobblini.net
Guest
Posts: n/a
 
      01-15-2007
Hi --

On Mon, 15 Jan 2007, wrote:

>
> On Jan 14, 2007, at 9:23 AM, 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

--
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)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why can't an instance instantiated within a class method access aprotected instance method? Greg Hauptmann Ruby 9 06-16-2008 10:16 AM
instance method adding another instance method to the class Raj Singh Ruby 2 05-29-2008 10:09 PM
TypeError: unbound method PrintInput() must be called with test instance as first argument (got test instance instead) arotem Python 4 10-17-2005 07:52 AM
accessing class instance variable from instance method David Garamond Ruby 5 06-08-2004 02:26 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57