Classes & Instances

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

In the 1 + 4 instruction how would you characterize 1, + and 4 object-oriented-wise?

1 and 4 are instances of the Integer class. + is an instance method of the Integer class. 1.+4 = 5

What does the private keyword stands for within a Class?

The private keywords draws a line between the instance methods that can be called from outside the Class, and the ones that can only be called within the Class. private methods are the ones under the private keyword (here #start_oil_pump and #init_spark_plug), they only have an internal purpose

What defines behavior and state in a ruby class?

The set of instance methods defines behavior. They can act on the state of the instance they're called on. The set of instance variables defines state. They store data characterizing the state of the instance (which is mutable).

Suppose we have a Car class with a @color instance variable and an attr_writer :color. Let's consider an instance of Car called my_car, how can you update its color?

You just have to call #color on my_car and assign it its new value:

Do you know what getters are?

getters are instance methods allowing you to read an object's instance variables values

What's the class constructor method name in ruby?

self

Do you know how to set both a getter and a setter in one line?

use attr_accessor to get read(getter) and write(setter)

What are the naming conventions for a ruby class (filename and class itself)?

Filename is in lower_snake_case, for instance: sports_car.rb Class name is in UpperCamelCase, for instance: SportsCar

In the numbers << 3 instruction where numbers is an instance of Array, what is <<?

<< is an instance method of the array class

What is the difference between a Class and an instance?

A Class is like a cake mold or a recipe. It helps to create a cake, but it is no cake. An instance of a class is a cake created from a given cake mold.

What are instance methods? How do you define them and why do we call them instance methods?

Instance methods are the methods defined within a Class We call them instance methods because they can only be called on an instance of the Class, and not on the Class itself.

What are instance variables? How do you set them and why do we call them instance variables?

Instance variables (ivar) are the variables that store the instance's values (data) of the attributes defined in the Class. Every variable defined in a Class beginning by @ is an instance variable. We often set them in the #initialize method

What does OOP stand for?

OOP means Object-Oriented Programming, and it's a paradigm based on a concept of objects which are composed of attributes (the data of the object, its states, even called instance variables) and its behavior, the instance methods.

Do you know what setters are?

setters are instance methods allowing you to update an object's instance variables values.


Kaugnay na mga set ng pag-aaral

Chapter 1 Notes - Chapter Recap Questions

View Set

Ch. 7 - Designing Organizational Structures; Ch. 8 - Managing Human Resources and Labor Relations

View Set

Chapter 28: The Civil Rights Movement

View Set

Chapter 9: Courts and Pretrial Processes

View Set