When we say object-oriented programming, we mean that our code is centered on objects. Objects are real-life instances that are classified into various types.
Let’s take an example to understand this better. If we consider a rose as an object, then the class of the rose will be flower. A class is like the blue-print of an object and describes the attributes and behavior of an object. the attributes of a flower could be the color, fragrance or even whether it has thorns. these features are part of the class and each instance of the class i.e. each object of the class would share these attributes. though the value of the attribute may vary among different objects. take for an example – a lily. So if the color of the petals of the rose object is red then for the lily it could be white. this is the basis of object oriented programming where we can take real life situations and make different instances from it.
Now let’s see on how we can define a class in Ruby
class classname end
Implementing OOP concepts with Ruby
Ruby supports the OOP paradigm by allowing the creation of classes and its objects. Like we said before, objects are the instances of a class and a class is like the blueprints for the object. A class lists out the attributes and defines the behavior for the object while the object is the real world representation.
A class has two parts to it – data and methods
So now that we have a class Language Let’s define the attributes as
Now we have our data but we need a way to access them. This is where our methods come in. class Methods are methods defined in the class that are used to access the various data in our class. For Implementing into code, we use a constructor, that takes in values of the attributes and assigns them to the space stored for the particular object.
Constructor’s Syntax :
def initialize(x, y) @x = x @y = y end
The initialize function is defined within the class and its syntax is just like an ordinary function. It can take any amount of arguments. The @ symbol represents the object’s actual attributes.
class Language def initialize(language_name, topic_name) @language_name = language_name @topic_name = topic_name end end
To create an instance of a class, we use a familiar function, which we might have used earlier for making hashes or arrays. We use the new function.
object_name = Class_name.new()
If we have any parameters, it is usually passed in the parenthesis of new, just like in ordinary methods.
So we have an object named as object1. We use the Language.new() to create a new instance, this calls the initialize function. So the object1.language_name is Ruby. And object1.topic_name is method
Let’s create a second object as well
So the object2.language_name is Scala And object2.topic_name is string.
Now that we have defined our class and created objects, we need to be able to change or view the attributes of our object. This is where the second part of our class comes in – Methods. Class methods are much like our regular methods defined within a class.
We use methods to change the value of some of our attributes and to view the values as well. To call the method for a particular method we have to mention the object_name.method_name. This is better understood with an example.
Language name for object1: Ruby Topic Name for object1: method Language name for object2: Scala Topic Name for object2: string
We have created two objects in the above example. object1.return_name calls the method return_name for object1, this returns the result ‘Ruby’. If we call object2.return_topic, it calls the method return_topic for object2 This returns the result ‘method’ to the puts statement.
Normally in ruby, methods return the last computer result of the method so we do not have to explicitly write return.
So instead of writing below code
def return_name return @vehicle_name end
We can write code as below.
def return_name @vehicle_name end
When we say variable scope we mean under which areas a certain variable can be used. The scope can be global or local When we say global we mean that throughout our code, we can use the global variable anywhere To define global variables we use the ‘$’ symbol.
Language name for object1: Ruby Topic Name for object1: method Language name for object2: Scala Topic Name for object2: string The reader is ABCD
In the above example, we declared a global variable Since this variable is global we can use it anywhere in the program. $reader responsible for ABCD. Let’s we tried this with one of the instance variables object1.language_name. This would have given an error because this variable is of local scope i.e. within the class only. so we need to use access methods or use attr_reader which explained below.
Let’s understand it with example.
Language name for object: Ruby Topic name is method New Topic Name is string
In the above example, we see a new method modify_topic. This method is used to change the value of the attribute topic_name. Since the attributes cannot be accessed directly outside the function, we resort to using methods to access them. As we can see in the example, we pass the new topic as an argument to the modify_topic method. Within the method, the object’s topic attribute is reset to the new topic.
Class variables are different from instance variables in the sense that they belong to the class and not the object. For instance variables like language_name and topic_name, we have one copy for each object but for class variables, one copy is shared among all the objects typically, the variable belongs to the class, not the instances of the class but it can still be accessed by the instances of the class. Class variables are usually identified using ‘@@’. The syntax is @@class_variable = 0. It can have a default value and that could be anything ranging from a string to an integer.
Language name for object1: Ruby Topic name for object1: method Language name for object2: Scala Topic name for object2: string The reader is ABCD The number of objects created is 2 The number of objects created is 2
In the above example, we have used a class variable count to keep track of the number of objects being created this is one of the most common usages of class variables in the above example, we initialize count with to 0. We also add a function to the return the value as and when called. every time an object is being created, the initialize method is called each time the initialize function is called, count gets incremented by one i.e. @@count += 1 regardless of whether we call the return_count method using object1 or object2, it prints out the same result. like instance variables, it’s scope is only within the class and cannot be accessed directly outside the class we use .to_s to convert the number into a string.
Instead of access methods
In the previous examples, we’ve introduced methods to return the attributes. We have an easier work-around for that. This is where attr_reader, attr_writer, attr_accessor comes into play consider the next slice of code.
The name of the language is Ruby The topic of the language is method The new topic of the language is array
- Object Oriented Programming in Ruby | Set-2
- Ruby Programming Language (Introduction)
- Comparing Ruby with other programming languages
- Interesting facts about Ruby Programming Language
- Difference between Functional Programming and Object Oriented Programming
- Use of FLAG in programming
- I Can't Use Logic In Programming. What Should I Do?
- If-Then-Else statement in SAS Programming
- Like Operator in SAS Programming
- Contains and Between-And operators in SAS Programming
- Where Statement in SAS Programming
- 5 Best Programming Languages For Newbies
- Introduction to Swift Programming
- Introduction to Programming Languages
- Swift Programming Language
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.