Question about Array's size instance method

Discussion in 'Ruby' started by Matt Gretton, Jan 14, 2007.

  1. Matt Gretton

    Matt Gretton Guest

    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/.
     
    Matt Gretton, Jan 14, 2007
    #1
    1. Advertising

  2. 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.
     
    Timothy Hunter, Jan 14, 2007
    #2
    1. Advertising

  3. Matt Gretton

    Guest

    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)
     
    , Jan 14, 2007
    #3
  4. Matt Gretton

    Guest

    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
     
    , Jan 14, 2007
    #4
  5. Matt Gretton

    Guest

    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)
     
    , Jan 15, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zygmunt Krynicki
    Replies:
    1
    Views:
    643
    Ivan Vecerina
    Oct 11, 2003
  2. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,742
    Michael Jung
    May 25, 2008
  3. David Garamond
    Replies:
    5
    Views:
    281
    Ara.T.Howard
    Jun 8, 2004
  4. Raj Singh
    Replies:
    2
    Views:
    218
    Rick DeNatale
    May 29, 2008
  5. Greg Hauptmann
    Replies:
    9
    Views:
    271
    Loren Segal
    Jun 16, 2008
Loading...

Share This Page