Objects, Instance, Variable and Class in Ruby (6 of 11)
Ruby for beginner series (6 of 11)
In the process of learning object-oriented programs, there are many terms that often make students confused, for example the understanding and differences between objects, classes, instances, variables, instance variables, class variables. So in this practice will be explained in detail the meaning and difference
Object, Instance, Variable, Class
- Object can be imagined as something that has characteristics (properties) and the ability to do something (behavior).
- Examples of objects such as humans, cars, birds,…
- As for Object, human have characteristics (properties): hair color, height and shape that are different from one another, have the ability (behaviour): able to run, scream.
- If there are 3 people, for example named John, Jenn and Anne, then the 3 people are said to be 3 instances of human objects.
- Then if it turns out that John has 2 names for example John and Josh, then it can be said that we currently have 4 variables for 3 instances
- So you can imagine that the variable is just a label attached to an object and one instance can have more than one variable
- If it’s for a car object
- A class can be imagined as a Blue-print of the car object to be created
Of course, the blue-print for each car factory can be different because making racing cars (Ferrari) and making cars to transport goods (Toyota) are clearly different. - If there are 3 cars produced by the factory and named red, yellow and green then it is called 3 instances.
- If the red car is also named for example mobil_joni, it means that there are 4 variables that point to 3 instances.
Get widjajayd’s stories in your inbox
Join Medium for free to get updates from this writer.
Notes for source code above:
- Class names must start with an uppercase letter (Uppercase).
- Naming techniques using Camel case for example Rectangle, Square
in the example program above there are one represented by the Rectangle class. - Two instances, because doing new 2 times where the first box is 10 long and the second box is 30 long, thus in computer memory there are only 2 places.
- There are 3 variables (box1, box2 and box3) where box1 and box2 designate the same instance, check box1 and box2 has same object_id (0x00007fbc0f8c9150)
Understanding member variables, class variables, and instance variables.
- In a class, there is a member variable. Member variables are divided into 2 types. Instance Variable and Class Variable
- Instance Variable For example, @length and @width and Remember Instance variables are different from instances and different from variables. This instance variable shows the characteristics of an object. For example, if the car has a color, type of fuel, price, If the rectangle has a length and width, If a triangle has a height and a base, A circle has a radius if it has one. Instance variable begins with an “@” in its name. Its properties are recognized by all methods (def) in the class. By default, all instance variables are private, unless defined with the attr_ command.
- class variable For example, @@total , For example, class variables are generally used to analyze data as a whole. For example, if a car factory produces 1 car, then the @@total variable is added by 1 so that in the end it will be known how many cars were produced. Class variable values are also shared between instances. For example, if a car factory produces red, yellow, and green cars sequentially, each of these car instances can access the value of the class variable.
- class method, for example, self.total_boxes in ruby there is also what is called class method. The way to declare a class method is to start with self. the use of the class method, for example we want to know the number of objects that have been created, without having to create a new instance, the number of objects generated can be checked by calling the class name accompanied by the method name.









