SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    New Instance of a Class Question

    Hi. Ok, I am new, and I'm sure my question is very basic, but I want to learn RoR and if I don't understand something in detail it bugs the heck out of me. So hopefully someone can help.

    I'm going through the Sitepoint book and come to this example:
    class Car
    @@number_of_cars = 0
    def self.count
    @@number_of_cars
    end
    def initialize
    @@number_of_cars += 1
    end
    end

    I created 4 car objects and then did a Car.count and got 4, so it works as advertised, but I have two questions.

    1. with the line of code @@number_of_cars = 0, why isn't that variable getting reset to 0 each time I instantiate a new car object? It seems to me it would, but obviously it isn't. Which is a good thing, but I don't udnerstand why it isn't resetting the variable to 0 with each new instance.

    2. I assume it is just 'convention' and the name 'initialize' that makes that method run each time a new instance of the object is created. Correct?

    Thanks.

  2. #2
    SitePoint Enthusiast toytron's Avatar
    Join Date
    Nov 2006
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    qoute from techotopia

    Ruby Class Variables

    A class variable is a variable that is shared amongst all instances of a class. This means that only one variable value exists for all objects instantiated from this class. This means that if one object instance changes the value of the variable, that new value will essentially change for all other object instances.

    Another way of thinking of thinking of class variables is as global variables within the context of a single class.

    Class variables are declared by prefixing the variable name with two @ characters (@@). Class variables must be initialized at creation time. For example:

    @@total = 0
    hope this helps

  3. #3
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! So I guess the theory is that the first time I create an instance of this class it will recognize that the global variable doesn't exist and it will create and initialize it. However upon creating the second instance it must jsut skip that line since it is already set. Otehrwise it woudl reset it to 0 again.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ccj64 View Post
    Hi. Ok, I am new, and I'm sure my question is very basic, but I want to learn RoR and if I don't understand something in detail it bugs the heck out of me. So hopefully someone can help.

    I'm going through the Sitepoint book and come to this example:
    Code:
    class Car
      @@number_of_cars = 0
      def self.count
        @@number_of_cars
      end
      def initialize
        @@number_of_cars += 1
      end
    end
    I created 4 car objects and then did a Car.count and got 4, so it works as advertised, but I have two questions.

    1. with the line of code @@number_of_cars = 0, why isn't that variable getting reset to 0 each time I instantiate a new car object? It seems to me it would, but obviously it isn't. Which is a good thing, but I don't udnerstand why it isn't resetting the variable to 0 with each new instance.
    When you post code, put code tags around your code to preserve the indenting.

    When you create a new instance of a Car, the only thing that executes is this:
    Code:
    def initialize
        @@number_of_cars += 1
    end
    In that method, @@number_of_cars does not get reset to 0. Ruby parses the class definition once when it first encounters the class definition in your code. That's when 0 gets assigned to @@number_of_cars. Thereafter, when you create an instance of the class, that does not cause ruby to reparse the class definition.

    2. I assume it is just 'convention' and the name 'initialize' that makes that method run each time a new instance of the object is created. Correct?
    When you create a new object of a class, the initialize() method in the class is called automatically if present.

  5. #5
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the clear explanation


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •