---
# System prepended metadata

title: Ruby Classes and Instances Discussion

---

# Ruby Classes and Instances Discussion


## Group 1

### What if we wanted to keep track of all of the computers that we create? Where would we store them and why?
We would store the computers in a class variable so that each instance has access to that information. 

```rb
@@all = []
```

Then we would create an instance method to push itself into the class variable`@@all`.

```rb
def save
  @@all << self
end
```

It gets called with `Computer.all` to return all of the computers listed in `@@all`

```rb
def self.all
  @@all
end
```

...
### What's the difference between a class variable and an instance variable?
A class variable can be used within the entire class, and the instance varibale is only accessible within the instance. Class variable syntax is `@@<variable-name>` Instance variable syntax is `@<variable-name>`
...

### What's the difference between a class method and an instance method? (think both about how they're defined and how they're called) 
A class method is a method that is called on the entire class. To define the method you have to use `def self.<method_name>` within the class. It is then called by using `<class-name>.<method-name>`

An instance method is only available within the instance. It is defined in the class using `def <method-name>` and is called using `<instance-variable>.<method_name>`
...

### What does `self` refer to within a class method?
It refers to the class itself. 
...
### What does `self` refer to within an instance method?
It refers to that specific instance, and only that instance. 
...
### Can we access class variables from instance methods?
Yes, because the scope of class variables makes it available to classes and instances.
...

### Can we access instance variables from class methods?
No, because the scope of instance variables is only available in that instance, but not the entire class. 
...



## Group 2

### What if we wanted to keep track of all of the computers that we create? Where would we store them and why?

...
### What's the difference between a class variable and an instance variable?
Instance variables are defined with "@". Class variables are defined with "@@" and have a wider scope.


### What's the difference between a class method and an instance method? (think both about how they're defined and how they're called) 

...

### What does `self` refer to within a class method?
The class

### What does `self` refer to within an instance method?
The instance

### Can we access class variables from instance methods?
Yes

### Can we access instance variables from class methods?
No

## Group 3

### What if we wanted to keep track of all of the computers that we create? Where would we store them and why?

classwide variable 
@@computer = []

### What's the difference between a class variable and an instance variable?

... a class variable is accessible to the entire class, class variables are used to store data that changes between instances. Instance variables are responsible for holding data within each instance and are available in scope for all instance methods in a class. 

### What's the difference between a class method and an instance method? (think both about how they're defined and how they're called) 

...with class methods we use self.method as a reminder that the method will not be running on a particular instance but will be acting as our main factory/blueprint class method(s). An instance method is defined within a class and is called when an object or class item is passed through the instances of that class. 

### What does `self` refer to within a class method?

... self refers to the class method that it was calld within 
### What does `self` refer to within an instance method?

...self refers to the that particular instance object.
### Can we access class variables from instance methods?

...yes. Parent to child analogy.

### Can we access instance variables from class methods?

...

## Group 4

### What if we wanted to keep track of all of the computers that we create? Where would we store them and why?
@@all = []

def save
  @@all << self
end
...
### What's the difference between a class variable and an instance variable?
class varible includes @@ where as instances only include one @.
Class variables are shared between subclasses and  the main class. 
Instance variables are only tied to one class.
...

### What's the difference between a class method and an instance method? (think both about how they're defined and how they're called) 
    Class method provides functionality to the class whereas instance methods provides functionality to one instance of a class.
...

### What does `self` refer to within a class method?
    Self refers to the class name. Call back for class.
...
### What does `self` refer to within an instance method?
    Whenever there's a new instance, this new instance or method refers to self and creates a new object as an example.
...
### Can we access class variables from instance methods?
    Instance methods can store new initialized variables or objects inside of class variables.
...

### Can we access instance variables from class methods?
    No you can't. When calling a method, it is of the class itself and not of the instance.
...