
In this article I will go into more detail about Classes and Objects. After reading this article will you understand:
- How State and behavior work?
- How to initialize a New Object
- Instance variables
- Instance methods
- Accessor Methods
If you would like to learn more about Ruby please check out my other articles
Day 1 :
Day 2:
Day 3:
Day 3 Part 2:
How State and behavior work?
When defining a class, focus on two things: states and behaviors. States track attributes for individual objects. Behavior are what objects are capable of doing. Instance variables are scoped at the object level, and are how objects get track of their states.
Instance methods define behavior in a class. Instance methods defined in a class are available to objects of that class.
Instance variables keep track of state, and instance methods expose behavior of objects. Every objects state is unique and instance variables are how to keep track of it.
How do you initialize a New Object?
Need to add a initialize method to each class, so when you create a new object the initialize method is called every time. The initialize method is referred to the constructor because it gets triggered whenever we create a new object.
What is a Instance Variable?
It is a variable that exists as long as the object instance exists, basically it is a way to tie data to objects. It is available until the object instance is destroyed
What is a instance method?
The purpose of a instance method is to expose information about the state of the object.
What is a attr_accessor?
It is a built-in way to automatically create getter and setter methods, it takes a symbol as an argument which is used to create the method name for the getter and setter methods.
How do you call a method with self?
self is used to specify a certain scope and can be referred to different things depending on where it is used.
- Use self when calling setters methods within a class
- Use self for class method definitions
self inside a instance method references the instance that called the method, self outside of an instance method references the class and can be used to define class methods.
If you need to change change a instance variable data by using a instance method. You need to use self.<instance variable> to change the data. If self is not used Ruby you think that you are creating a local variable instead of changing the data in an instance variable.
What is a class Variable?
Instance variable that captures information related to instances of classes. To create a class variable use @@. Is used to keep track of class level detail that pertains to the class and not to the individual objects.
What is the difference between class and instance methods?
instance methods: pertain to the instance or objects of the class
class methods: methods that can be called directly on the class itself without having to instantiate any objects. When defining a class method must attach the method name with the word self.
Thanks for reading and in the next article I will include some examples on OOP concepts.