Classes and Objects Flashcards

1
Q

What do ‘states’ do?

A

States track attributes for individual objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does ‘behavior’ do?

A

Behaviors are what objects are capable of doing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How are instance variables scoped?

A

Instance variables are scoped at the object (or instance) level, and are how objects keep track of their states.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Describe an instance method.

A

Instance methods define the behaviors in a class. Instance methods defined in a class are available to objects (or instances) of that class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the initialize method often called.

A

We refer to the initialize method as a constructor, because it gets triggered whenever we create a new object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is an instance variable?

A

An instance variable is a variable that exists as long as the object instance exists, and it is one of the ways we tie data to objects. It does not “die” after the initialize method is run. It “lives on”, to be referenced, until the object instance is destroyed. Every object’s state is unique, and instance variables are how we keep track.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What do instance methods do within a class?

A

Instance methods allow all objects of the same class to have the same behaviors, though they contain different states.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is attr_reader?

A

This is a getter method that takes a symbol as an argument. An example of how it replaces the code for a getter method is:

Getter method:
def name
@name
end

replaced by : attr_reader :name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is attr_writer?

A

This is a setter method that takes a symbol as an argument. An example of how it replaces the setter method is:

Setter method
def set_name=(name)
@name = name
end

replaced by : attr_writer :name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is attr_accessor?

A

This is a Setter and a Getter method which takes a symbol as an argument.

attr_accessor :name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why not just reference the syntax for instance variables (@instance_variable) when using a setter or getter method?

A

Technically, you could just reference the instance variable, but it’s generally a good idea to call the getter method instead.

ie) “#{name} is old.” = getter method
“#{@name} is old.” = instance variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How many methods does this line of code give us?

attr_accessor :name, :height, :weight

A

6 - (name, name=, height, height=, weight, weight=)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you disambiguate setter methods and creating a local variable?

A

Prefixing ‘self’ to the setter method name (i.e.. self.name). This tells Ruby that we’re calling a setter method, not creating a local variable. To be consistent, we could also adopt this syntax for the getter methods as well, though it is not required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are class methods?

A

Class methods are methods we can call directly on the class itself, without having to instantiate any objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Why do we need a class methods?

A

Class methods are where we put functionality that does not pertain to individual objects.

def self.what_am_i                # Class method definition
  "I'm a GoodDog class!"
end

GoodDog.what_am_i # => I’m a GoodDog class!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are class variables?

A

Class variables create variables for an entire class, just as instance variables capture information related to specific instances of classes (i.e., objects). Class variables are created using two @ symbols like so: @@class_variable..

17
Q

What are constants?

A

Variables that you never want to change.

18
Q

When is to_s called?

A

‘puts’ and string interpolation automatically call to_s on the object. May need to override the default to_s for these. However, it is also called with ‘p’, but this should not be overridden as it is useful in debugging.

19
Q

What are 2 use cases for ‘self’?

A

1) Use self when calling setter methods from within the class. Using self allows Ruby to disambiguate between initializing a local variable and calling a setter method.
self. name = ‘bob’ - calls setter method on instance variable ‘name’
2) Use self for class method definitions.

def self.what_am_i                # Class method definition
  "I'm a GoodDog class!"
end

GoodDog.what_am_i # => I’m a GoodDog class!

20
Q

What does an instance method return from within the class when it calls self?

A

From within the class, when an instance method calls self, it is returning the calling object.

self, inside of an instance method, references the instance (object) that called the method - the calling object. Therefore, self.weight= is the same as sparky.weight=, in our example.

21
Q

What is self referring to when used a class method?

A

self, inside a class but outside an instance method, is actually referring to the class itself.

self, outside of an instance method, references the class and can be used to define class methods. Therefore, def self.name=(n) is the same as def GoodDog.name=(n), in our example.

22
Q

What is a class?

A

Ruby defines the attributes and behaviors of its objects in classes. You can think of classes as basic outlines of what an object should be made of and what it should be able to do.

23
Q

What are objects?

A

Objects are created from classes. Think of classes as molds and objects as the things you produce out of those molds.

24
Q

Why do we need to use self with setter methods?

A

To disambiguate from creating a local variable.