Class and Objects in Ruby

Testing Solution
A free video tutorial from Testing Solution
QA Lead professional and Freelance Trainer
4.0 instructor rating • 8 courses • 4,002 students

Learn more from the full course

Web Automation-Selenium-Ruby|E-2-E Cucumber integration-15hr

Detailed ruby programming | Bdd with cucumber | Test Unit and Its usage | Automation Framework |

15:34:06 of on-demand video • Updated November 2019

  • ** Ready to automate any web application using Selenium and Ruby ***
  • ** Detailed understanding of Ruby Programming concepts ***
  • ** Ready to implement knowledge in realtime projects ***
  • ** Ready to face Selenium-Ruby Interview questions ***
English [Auto] So the previous session we have seen how we can write our code into a file and how we can execute that. But as we know Ruby is an object oriented programming language so because it's an object oriented programming language is it follow all the hoops concept and the first concept is and get Felician and conflations says whatever the code we are going to write. That must be inside a container or we can call it that must be inside the body. We should have the starting point and the end point of the code to implement that and get politian we have the option of classes in Ruby. So whatever the code we are going to write we will write inside the class boundary by this class. We will define starting end and boundary of the code. So here what is class in Ruby. So class is a container of variables constants methods and constructor variables means variables are used to hold some data and value of the variable can be changed. Constants are used to hold some data. Well the constant cannot be changed. Method is a group of code to perform some specific task and constructors are again quite similar to the method which are automatically called when the object is created. So as of now I'm just giving you a basic definition. Later on we will understand all these terms in a very detailed manner. So if you talk about the class of classes the container of variable constraint method and constructor when we are going to create a class in Ruby. The class must always be start with a capital letter so that we need to remember. Class body and with and keyword. So we are going to use this keyword with the name end to end the body of the class. We can access class member by gridding. Object of the class. So what you are going to write inside the class. We can access it anywhere by creating objects of the class. So in this session I'll show you how we can create class and how can we access class members by creating objects just moving to a new file. And here I'm going to save this file with the name Class A dot RB So whatever the name you are going to write of the swine. Just remember this name. I'm just giving it so as of now. I've just created the scripting file. Ruby scripting file and given the name class it or RB. Now here in this file I want to create a class to create a class. The keyword is class whatever the name of the class you want to give. So I'm just giving the class name a. You can use any name and as we have gone through whenever you want to add to the class you need to use the keyword And so that's the starting of the class and that's the end of the class. Whatever the code that you want to write inside the class we can write it all here. Here we can define where both constants methods and constructors as of now I'm just going to define a method as simple method which is going to display some value and I want to show you how we can access these methods by creating objects so I'm creating a method where we are creating a method by using the word def or does the method name you want to give. I'm giving the name is Al method name is also and with the curate and like that the body of the matter starting from here. Here. So we should have like method body will be this now whatever the code you want right inside the method like I'm riding boots and I'm just saying this is class method. So everything is done like I have created a very simple class with a name a. I've created a method inside the class and that's the end of the method and the class. Now as we have gone through all the class members means method is a class member as well. If you want to access these members we can create an object and call it method so how we can create objects so I'm coming outside the class to create object. We just didn't write object name is equal to class name dot new keyword. So we are using the word anyway. And to call the method. I'm just using it. That's my object name. And what is the method name. So my method name is hello. Everything is done. I'm just going to call this moving to the command prompt and I'm going to execute this file so Ruby and the file Lemus Class 8 or RB As I enter. So if you notice it is calling my method so this is class method. So what we have done here we have created a file with the extension whatever the file we are going to create here in the Ruby programming extension must always be our B. So I have created it will be fine. But now I'm not writing the code but actually I created a class an inside the class and writing the code in a format of methods. And now whenever Do you want to call the method method call it can be done by using by creating object object Redding's indexes. Object Name is equal to class name dot nil. And here we create object of class A and to call the method. We just need to write object dot method name here. If you notice we are not writing these records so I'm just coming here. We are calling method one more thing. Now I have written these brackets. If I go here and exit cute Still it is working means when we're calling any method and test is not having any argument means we did not define any argument here. So when you're not defining any argument not to go for decades and in the same way when you're calling it if you're not passing any argument so we need to use any brackets. So if you're right it's that fine if you don't write it again it's fine. I'm just writing it again. I'm just calling it again. It's working. So as of now I was sure you were grading object of the class in the same file and calling it I just remove this. So now we have a class with the method but we do not have calling for it. And I'm coming here just running it fine. You will notice nothing is happening because classes there method is there. But we did not call that method for nothing is executing. Now I'm going to create one more file on the same location. So I'm going to create one more Ruby fine just searing it and b dot. RB So I have created a file with an M.B.A. or B. Here I want to call that method me. Now we have two different files and both the files are in the same location as of no. So whenever you want to use any other file. First of all we need to import it if you want Java. So we are writing the statement like import and package name we are given the package number there. If you want on C++ or C so hope you remember at the top of the file we are writing hash include as did your dot edge means we are importing our library file in the same way. In Ruby whenever you want to use any class which is written in any other file. So here we have a class A which is written in the file. Class it. RB. So first we need to import that file so we import files we need to use word require and here. Which way do you want to use Class A so because I'm just giving the filename my file was Class A. Now I want to create the object of the class create object of the class or G is equal to it. DORTE new. That is the class name we are creating object of the class and the call method or B just dropped hello and here the method is hello I'm going to exit could this I'm executing file with the name. Don't be because we're calling it here. And it is going to fail. Why I didn't show you it is sharing like we are using you are require where we are not able to load the file with the name class. It is not able to find all the Class A. Now I want to show you something I'm just creating a folder. I'm just moving to my system and creating a folder with make project name like this. I'm writing my project and both the files which I'm using is B and class A. I'm just putting all the files here. Right. And I want to exit it now so my intention is to show where all the files which I'm creating belongs to a project and I have placed all the files on one location. Now I want to execute this file so I'm executing like as you know Ruby and I'm executing B. RB We are going to get the same error again like it is not able to find out where this class exist. Even we know both the files on the same location. How we can make it understandable through I'm going to this file be what we're doing. Non-missing require this file but before the filename I'm using brought forward slash dart forward slash means on the current forwarder. As of now we both are on the same Forder. So dart forward forward slash means on this forward. As of now on the current folder where I am. So this b file is in the my Bridgett folder or something on the same project folder. Fine just fine. So now on the same folder second file exist I'm going to execute it now and just was clearing the screen and executing it now. And you will notice everything is working fine. And this is very important because later on that will be the architecture that we need to follow. I'll show you one more option class A which I'm calling I'm creating a new folder with the name lib and I'm basing this class into this lapful. No. Again I tried to use this and it will throw an error because in the current location we are not having that file. How to solve this. So now dart forward should the current folder but in current folder we don't have this class. So what we need to do inside this we have a folder with the name lib and inside the lib you cannot inside the class. So go to current folder then lib folder there you will get the file. Everything is done. I'm just saving and executing it again. So here we can see again we are able to use it. So whenever we are using any class we need to import the fine. If I'm using a class say so filename it's class A. So we need to import this file and we need to give the path to relate to part that's a current project inside the current project. Or we can call it current folder go to this location. There we will get the file. And inside that file the glass is placed which I want to use. So inside this wine glass is placed I'm creating objects of a class and then calling its methods. So here we have seen how we can create a simple class. And as of now I'm just creating a very simple matter in which we are just printing something and create the object called these methods. So that's a very simple architecture. I have generated. That's all we have for the session tanks watching this we do.