You are reading the article Examples And Classes To Implement Objects In Ruby updated in October 2023 on the website Vibergotobrazil.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Examples And Classes To Implement Objects In Ruby
Definition of Ruby ObjectsWeb development, programming languages, Software testing & others
How to Create Objects in Ruby?To create an Object in Ruby we can use the new keyword with the name of the class like chúng tôi the below example of the Object creation in Ruby. Let me explain the below example.
We have defined a class with class name ABC, We can see that we have used class keyword before the class name(ABC). We will use this class name to create objects using a new keyword.
Inside the class we have used a method initialize method, it is a predefined method that does the initialization.
Finally, we are creating an object from the class ClassName with the help of a new
When we create objects from the class at that time the class will also initialize the method automatically and do the work of initialization of variables. Here Object contains all the variables and methods of the ABC class.
Please see the syntax below for the class creation in Ruby.
class ABC #Do the initialisation here def initialize(param1, param2) # Here we are initialising the variables . @param1 = param1 @param2 = param2 end #Write here your methods and constants end #Create object from class ClassName A1 =ABC.new(param1, param2)#Creation Of Object A1 A2 =ABC.new(param1, param2)#Creation Of Object A2 A3 =ABC.new(param1, param2)#Creation Of Object A3 A4 =ABC.new(param1, param2)#Creation Of Object A4 How do Objects Work in Ruby?If you have seen any civil engineer designing or drawing some architecture for the buildings for architecture for bridges. Once a civil engineer draws any design that design can be used to create many buildings. Because that drawing contains everything which we need to create any buildings or bridges. In the same way objects in Ruby play the role of buildings and all the classes play the role of paper of drawing. Every time we create an object and the object contains the properties of classes. We can explain the working of class in Ruby can be explained with the help of the flowchart below.
Here we have one class with the name ABC and this class contains a method and a constant.
Here the new keyword will notify the Ruby compiler about instruction for the creation of an object from class name ABC.
Now we are creating many objects from the class ABC like object A1, object A2, object A3, and object A4.
Each object created from this class contains the same method and constant inside it. This means at any time if needed these objects can access the method M and variable C.
See the below flowchart of Ruby’s class for better understanding.
Examples
First, we have created a class with the name Person.
Next, we have created a Ruby inbuilt method initialize which will be used as the constructor inside the Ruby class to initialize basic things.
We wrote a method display_person inside the class which will display the data for the attributes passed to it.
Next, we are creating an object from the class Person with the help of a new keyword and calling method display_person with required parameters to display the person data.
Example #1Please see the below example of code along with a screen of output.
Code:
class Person # Method for initialisation inside the class def initialize() # Initialising work puts "Initialisation for the class Person will be done here" end def display_person(name,age,sex) puts "The name of the person is #{name} and age is #{age} also he is a #{sex}" end end # Creating an objects and passing parameters for initialization personObjec1 = Person.new() personObject2 = Person.new() personObjec1.display_person("Ranjan", 31, "male") personObjec1.display_person("Manisha", 28, "female")Output:
Example #2
First, we have created a class with the name Person as with help of Person class we will create objects using a new keyword.
Next, we have created a Ruby inbuilt method initialize which will be used as the constructor inside the Ruby class to initialize user details for use in other methods.
We wrote a method display_person inside the class which will display the data for the attributes passed to initialize at the time of object creation.
Next, we are creating an object from the class Person and calling method display_person with required no parameters as the parameters will be used which we have passed at the time of initialization.
Please see the below example of code along with a screen of output.
Code:
class Person # Method for initialisation inside the class def initialize(name ,age ,sex ) # Initialising @name = name @age = age @sex =sex end def display_person() puts "The name of the person is #{@name} and age is #{@age} also he is a #{@sex}" end end # Creating an objects and passing parameters for initialization personObjec1 = Person.new("Ranjan", 31, "male") personObjec2 = Person.new("Manisha", 28, "female") personObjec1.display_person() personObjec2.display_person()Output:
Recommended ArticlesThis is a guide to Ruby Objects. Here we also discuss the definition and how to create objects in ruby along with different examples and its code implementation. You may also have a look at the following articles to learn more –
You're reading Examples And Classes To Implement Objects In Ruby
Update the detailed information about Examples And Classes To Implement Objects In Ruby on the Vibergotobrazil.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!