Transcript for:
Lecture on Ruby and Ruby on Rails

[Music] [Music] [Music] okay first ruby and ruby on rails you can see my screen right yes we can yes so what is ruby according to you this i mean my sessions are mostly interactive i would uh love anyone to answer my questions okay so what is ruby and ruby what is ruby on rails according to you ruby is the basic concept of programming and ruby androids that's a platform we are working on exactly so ruby is the language and ruby on rails is a framework which is built in obviously the language to be so language and the platform or the framework ruby on rails is a framework just like any other framework like spring stretch in other languages django your android sdk that's another framework or a platform so like this this is a ruby on rails framework so why do you need a framework you already have your ruby but why do you need a framework or a platform as you said so why do you need a framework can anyone answer this for ease uh for why do you need a framework yeah for ease exactly for ease any other any other answers you can think of right so basically it's for ease developer is right it would make the developer job happy i mean it's a free build framework so that you can do specific right specific parts of code in specific parts so that you can have your working application that is developer is as you say right so a simple example or a basic example if you take a photo of yourself or photo of your it would look better in a frame frame right a photo frame because it has i mean durability increases the durability east aesthetics and portability etc etc like that this framework using a framework gives you a number of advantages so as part of this curriculum we'll first learn the language ruby which is required for performing ruby on rails creating or adding adding additions or creating new features in routine rates so second part will touch on ruby on rails creating a rail certification etc right so coming to ruby language basically it is a interpreter based language so what is an interpreter based language interpreter based language is a language where instructions are read line by line so if you have done in academics or somewhere c programming you need to write the whole program and then compile it right and you will get a class and by code file and then you have to execute it so two parts are there in compiler based language but ruby is not a compiler based language it is a interpreter based language so line for example this is an instruction in ruby line 1 and this is an instruction in ruby line 2. change into byte code happens line by line so execution is line by line and there is no compilation okay so interpreter based language ruby is an object oriented programming language now here this is the most important part of ruby what is object oriented programming very basic when i if one liner is enough can anyone answer that according to what is object oriented programming everything is an object and every object has attributes and has properties exactly so why do you need to have an object and attributes so i don't don't repeat my work i can i can make as one class and inherit this class for many many times right exactly so this is at a programming level right so what at the design level or i mean whoever is creating new applications right so earlier they were only c programs right c programs and again cgi that to internally return in c etc so later on came this concept of object oriented programming c programs as you can see is procedural programming procedural language so instructions are written continuously line after line pine after line functions like previous javas previous versions earlier versions of javascript earlier versions of javascript in the sense like before object oriented javascript has come into picture since a few of you know javascript right so earlier versions of javascript did not have any kind of object oriented concepts built into it so that kind of a programming okay so when they were doing this procedural programming right they found like enhancing the existing application or making changes or bug fixes etc was really a headache it was very cumbersome to do so so they came up with this object-oriented programming languages concept programming concept it's a concept where you could represent a particular entity by means of a class okay and add attributes to it etcetera etcetera so let me explain this in a bit detail so that people with no programming background right into objective and would understand better okay for example as i can see so hunger station works on it's a majority of major food delivery apple yes so what all entities are required to build this application for example this is not a software application take into consideration hunger station is not a software application but it is a store hunger station is a store for example so which delivers your customers can call and delivers for example so what all entities are required by hunger station can anyone the first working professional right right yes like um delivery boys and store operators right so these kind of guys you need phone contacts of delivery partners delivery partners food partners some food joint partners etc and many many many other things okay basically working professionals you can say as users and customers right probably this is most of you know of this but you for example we need these kind these entities this is just a few you require a number of entities as you know but these are a few right so now you're trying hunger station has grown in from a store to a huge business so that you require a software application now so for creating a software application these entities you want to represent in object oriented programming languages okay so what i have to do i have to represent a particular for example take user or customer for example customer so customer coming to hunger station what are his attributes and what are his actions what do you expect a customer from coming to hunga station to yeah so two and what what do you expect him to do creating a new order exactly creating creating new order anything else text takes for example these are this is the action and these are the attributes so let me put attributes so actions performed so this non-technically we call what customer has this technically we call attributes or variables or member variables most more precisely variables are member variables actions if are called technically right functions or methods [Music] so who is the entity customer is the entity what he has are what you declare as variables three variables and what he does are declared as methods or functions right any questions on this simple concept um actually i came first from a little bit so i'm trying to pull off guys this is just yeah this is just an example we'll not write any code i still i try to get on what you what you also needs cars for example they are delivering on their own okay so according to you what i am writing a car class what a car has what does the car have oh soft on an address a physical car what does it have an engine yes exactly so a car has an engine or a steering steering wheels etc right etcetera etcetera lamps head lamps bonnet etcetera etcetera so what does a car do moving yes um this is what does a car have but the question is i have and this is sorry i'll pull this up a what does a car do as you said moving uh we can say something like ax rate rate stop start switch gear if it is in manual okay this kind of stuff right so these are nothing but you declare them as methods in programming languages in any programming languages okay the functionality is defined by functions so what does a car do is actually a function what does a car do is actually called variables what does the card work on methods are functions right now it's clear yes so i'll simply write a ruby class see we don't know ruby will not start ruby yet but even then i'll try to write it so that you could understand class class is the key word to create a class end is ending the class this is how we write a class if you want to write it in a new tab here i am using sublime text sublime text 3 is also released sublime text as my editor you can use ruby mine which is a paid editor which is very good sublime text and even atom you can use most of them use ruby mine either ruby mine or sublime text okay you can use even notepad clusters etcetera okay so here i am writing a class class ind this is i save this as on my desktop ask r dot rb rb is the extension for a ruby file exactly equal to b5 rb so saved it this is a car class and this is a ruby fight so where is my local file located on the desktop right so let me check another question i'm on what systems are you using what oss what is the os that you're working on mac mac okay everyone is mac yes yes good because ruby on rails applications are best supported on mac it is always advisable to use mac if not mac any linux flavor especially ubuntu okay but since you all have mac by default you would have ruby right so you can just type in can you can any one of you or all of you type in and simply say ruby hyphen v on your terminal just open terminal and type in a ruby hyphen v space hyphen v what is the version you get uh 2.7 i updated today so everyone have upgraded okay okay so your project specific version 2.3 2.3 okay got it so i guess so ruby is already installed in your machines anyone who doesn't have ruby installed in the machine problem if okay have a different version right here we have different versions 2.6 okay okay good so everyone has ruby in their machines see if at all you're working on multiple projects for example okay then you need to install something called rvm ruby version manager let me find out most of you might know about it you don't know manager so why do we need this ruby version manager first of all ruby version manager for example hunger station is working on ruby on rails 5.3 do you know the version of rubio is used by hunger station actually it's two point five point two point five two point five three uh no no i'm not talking about the ruby version i'm talking about rails version okay so you can say rails hyphen v for that come on here you can't get that but in your project there will be a file called gem file for example for example this is a ruby on rails application you'll be finding a file called gm file right so here they would specify the rails version do you know what kind of a version it is for hunger station yes 5.0.5 okay it is rails 5 got it so imagine you're creating a newer version of newer uh sorry older version of rails application you got an application to work on so that rails application is on i'll just write it here the trails application is on version three dot two okay and you are contracting another application working for another application which is rails version two two 2.0 or something and hunger station rails version is 5. so you have three rails applications and you have a single laptop imagine this is the scenario three applications three with different versions and a single laptop the problem is rails version 5 requires minimum 2.2.2 ruby version okay similarly this requires ruby 1.8 it doesn't work with ruby 2.0 2.0 this one minimum requirement is something like ruby 2.0 or 1.9 x okay so you have to each and every rails application requires a different ruby version because the rapid improvements made into rails and ruby if you want to work on a rails 3.0 application with ruby 2.2.2 version it is not compatible your application the application will not work got it so it means in your laptop or in your desktop you need multiple versions of ruby right agreed everyone is following yes so in order to install multiple versions of ruby right i can use rbm to be version manager so if at all you have the requirement you can simply do this first for installing rpm with default ruby on rails in one command you can do this or it'll simply ruby you can use this single command on your terminal that would install rvm on your machine once you get rpm installed i can say rpm list since i work on multiple projects a different version i have right from 2.1.1 to 2.6 now as sure as one of the passive pins said that they have upgraded to the latest ruby version 2.7 right i can try doing that rbm install 2.7 if it is the correct binary it would install otherwise it would not so ruby 2.7 binary it is not able to find i have to search it by myself rvm 2.7 4.7.1 binary so no binary binary ruby is available for os x 2.7 let me try for 2.7 quickly if it installs we will install otherwise we'll skip that yeah found remote file so it found 2.7.1 so it would install it so then if i say rvm this trade will get 2.7 not yet since it is still installing it it will take some time so understood so if at all you have that requirement you can use rvm any questions on this so far any questions on this so this um the rvm could be a should be a specific version right yeah you can install it any specific version rpm institute install is the command and for example you want to install ruby 2.0 simply say ruby right hand 2.0 okay if you want to install 2.2.7 2.7.1 so if it doesn't find for example if i say 2 dot 59 etc which it doesn't exist it will say i am not able to find the binary so then you have to google search and see what is the correct binary and then you can install it or else simply you can find the root download ruby's there right in that you can find the specific version so download ruby lang or downloads you can see this right yes people who are working on windows can use ruby installer but since your mac right you can it's already installed or you can use rpm stable releases you can see ruby 2.7.1 so this binary will be found so simply you have to come here rvm install and say ruby hyphen 2.7.1 this binary or else you want to install 2.6.6 simply write 2.6.6 like that if you write anything like 2.7.0 it won't work because there is no binary for it here right in the stabilities got it okay still configuring okay okay let us come back to our original discussion of class car so this class car oh sorry to interrupt this it's not running for me by the way for rvm which one this one yeah for uh for rpm can you send me this camera this one yeah yes uh can i know who's this ashraf this is omar omar okay so or i just think that for everyone you can come and put it on the terminal whatever you want to install okay and later on once rgm installed you can type in haphim install okay but see if you are thank you so there is another catch for it your can anyone open your hunger station jump file and see the gem version right and there will be ruby version as well in the jam file what what ruby version do you find there in here just have a range will have ruby version also in the jump file this so it's different from the terminal if i take it out from determinant it's different than the jump pi [Music] no this is the question no no i'm asking you to open the the hunger station app will consist of a jam file right gem file in that what is the ruby version you have what is specified in your application our application is 5.1.6.1 2.3.0 okay no two points two point six okay just like this okay good so yeah for me for me i don't know the guys uh so it is ruby 2.6 that you are using for your application right yes yes okay so it means once you install rvm you can't simply work on your application you have to switch to that specific ruby then so our vm use which one we have to use right this one we have to switch your ruby in here first and then say rails is whatever commands or whatever you want to work on rails right so your application requires this you have to switch your rdm or ruby version to the specific version then only your application will work okay if at all you're doing it via rvm if you're not using any kind of an rpm no problem you can directly work on your workstation your laptop okay any questions or confusion in this uh yeah i have questions yeah yeah so uh this will change for all directory right i can't having two different version for two projects running in the same time if that's possible so i can't use 2.5 something in one directory and 2.7 yeah another one i can't i can do this or i have to sit it as default for all the directories no you can switch it no one terminal you can switch it to one version one directory and another terminal you can switch it to another rails version okay so i can use two different version of ruby right now in two different terminals yeah yes same say at the same point of time you can execute two applications simultaneously different versions you can do that yes okay that's cool right any other questions oh now come to our class car this is ruby that we are writing here hash is a command single line comment in ruby i want to comment code in proving okay something like this these are variables right what the car does accelerate so that should be written in it is written as a function right in ruby how do we write a function or a method it is using def keyword emd is ending the method one small question here yeah for variables uh we don't have to define the type of it so directly we we write the variable name and assign a value to it so it will it will define it automatically yeah i i'll i'll come to that this is just an example next next i'll come to that okay how to define that set here because in ruby we have multiple i mean nearly five types of variables so i'll come to that so defining a method d e f and method name followed by n this is how we write a class in ruby any confusion c is small so lowercase class car class names are always capital start with capital okay df keyword to define a method endnd method this end is for class class ending right any questions as simple as that create a class curve any questions on this so far how to write a class in any language it would be the same yeah sorry please uh the last end for first one class uh car exactly this is the end for class ends here and method ends here right got it this is method n and this is class n okay okay we have car class so in as us as we have discussed customer or a car why do you create this is a protractor of the real car we want to show that real car or write that real car in software or ruby using class you want to represent that so what it has variables what it does is what it does is your methods okay so you've done it so there is another concept so what is the definition of a class basically what is a class definition i say definition of a class class is a prototype for something what is the something that something can be anything for example you know car customer laptop any object anything anything okay class and an object anyone knows what is an object what is an object what is an object these two terminologies are pretty important for any object oriented programming once you understand this concept right your rails programming or movie programming is pretty simple that is what i'm stressing on this part okay what is an object anyone exactly so the definition goes like it's an instance of a class so what does this mean the literal meaning of an instance of a class is instance means it has all the properties and behaviors that is functionality behaviors or functionality of the class okay so it has all the properties these properties behaviors of functionality is this of the class okay if that is the case can you give me any example of objects of car class what is an exam any example of an object of carcass can anyone tell this answer this maybe suv um suv okay anything else press press transmission transmission see okay see suv is type of a car okay but i am asking any object of a car class so it means object of car class or instance of a class for example i say i am an instance of my parents for example i am an instance of my parents it means i have their features their features they are like their eyes their nose we get we get some physical features from our parents right so those are properties some behaviors for example i walk like my dad i talk like my mom these are behaviors that we get from them this uh this cars is the main class and uh any any kind of car exactly yes any kind of car i mean any car on the road is an instance of this car class because it has one steering four wheels engine it can accelerate decelerate stop and start right so any your hondas your camry's or your message any any car which has four wheels one steering engine which can accelerate start stop is an object of this class understood the concept [Music] anyone who did not understand this concept because this is pretty important for us okay i assume everyone understood about it so another question any example of objects of customer customer class i mean we are the iba customer example yes any user for example john etc etc right any person who is a customer physical person is an object of this customer class okay right how can i execute this i'll comment this otherwise through how to execute it is on my desktop so so desktop ruby car dot rt it has executed i did not print anything so not give me anything it has executed without any issue how do i create an object of this class create object of class [Music] so class name dot mu is nothing but your object i'm not q that's it this is a class car the entire thing this is an object honda this is an object camry okay let us execute yes put test is printing something onto the console put string it is called put string on the footstring camry first one is obviously first one is honda right because i printed honda second one is camry because i printed cam car 1 and car objects so what did we learn create a class in ruby and how to create an object in ruby any confusion in this small concept inside the class itself after we define the plus right right so see objects you are defining outside the class object runtime yes it's outside the class yeah would be generally yes so so we have honda here so how many wheels do you think honda has i mean according to this track for example this i make into six how many this how many wheels this honda have four now six because this is an instance of this class right six right yeah you got the point right can this honda start yes because we have a start method can this honda break no no because we don't have a break method here right if i have a break method in here like this then it can break so you understood the concept right good so now i want this honda to start i'll see dot star right camry dot start started so both of the cards have started right so here if i honda dot start who is calling the start method it's a cluster on the object the objective is across yes honda object is calling the method start of the catalyst next camry object is calling the start methods it means that single method is being used for by multiple objects for execution right see in your stage station app multiple customers would be there i mean number of customers would be there right but your method would be only single one user dot payment or user dot order kind of right where the user or object user object might be different one one time it would be david who is ordering calling the order method another time there is abdul who is ordering a calling the order method another time rama is calling and other method the same order method so definition is single the method definition is only one time declared but multiple objects can call that method right object-oriented programming concepts okay understood in here yes right now let us start with learning ruby can everyone type something called irb on your terminal irb this irp it is called interactive rho b i for interactive rb is for ruby interactive ruby yes this is the console yeah ruby console or we can call it lc i think no rails is for a rails application that's a console for rails application here irb is just ruby console you can only perform ruby instructions here you can't perform rails instructions here okay okay so here is we start with practicing ruby kind of interactive okay everything in ruby is an object ruby is an object-oriented programming language everything in ruby is an object one statement to remember which i forgot to tell you is everything in ruby is an object see this is just vaguely said but not everything in ruby is an object because we have methods which are not objects okay we have certain operators which are not objects so apart from that everything in ruby is an object so everything in ruby is an object means what for example i say x for example i say hundred what is this what is hundred in programming a number right more specifically an integer right so everything in ruby is an object so it is 100 an object is it an object or not i mean vaguely you don't know but you can answer vaguely this is okay yes it is an object why is an object why is it an object because everything is object nice nice answer yeah but how do we say that it's an object can we prove it because he it has a attribute it's a number in it any other way i can prove that this is an object what it's like one one for example like what 50 plus 50 equal right equal 1000 it will be like it will be uh mess with them could be object okay okay let me clear off this uh so i am saying that ruby is an object i mean even you are telling that 100 is an object right so it means what does this mean according to this concept what we discussed about we are saying 100 is an object right so it means there might be a class for this android object right for example integer pnd agreed agreed it might so it should have a class for this 100 uncheck let us check right so dot class is a method which would return the class of that object integer okay 23.34 what is this this one float or decimal now my question is is it an object again yes yeah yes exactly why because if i inspect this class by saying dot class method it is giving this float float class so it means in ruby there is a class float right end okay now let's take in string is this an object again yes yes because it is belonging to a class string now coming to this um i mean one of the positive friends has asked me like ruby i mean in ruby don't we define the type of it right no we don't define the type of fit because ruby doesn't have this types data types because why doesn't it have data types because it is everything is object object this clear supposed your doubt what yeah does it clear your doubt okay yeah yeah okay so ruby doesn't have data types because everything in ruby is an object it belongs to a particular class fine what is what is this then yes javascript is complete no it's an array and what dictionary in other languages we will discuss each and everything for uh no novice professionals but you understand what this is a string everyone knows that this is a string this is a floating point number and this is an integer that knowledge you have right yes right great so so everything is an object in ruby 100.class gave me integer integer is a class right again this is an object class does it excuse me super class anyone who doesn't know what is a super class i'll just describe it now i don't know okay okay got it so someone talked about uh called suv right suv class s-u-v e-n-t this i declared a class suv does this suv have four wheels four wheels for now does it have four bits suv yes yes one steering one engine accelerate start stop this brake so does suv perform all of these yes yes yes so i can say car is a parent of suv he is a parent this is represented sorry car is suv is a child of trust car okay or i can say subclass child or subclass or derived class of similarly suv or car can be called base class or super class basically the point is suv is a child of car class why because i can represent the same statement in ruby directly by class suv subclass of card it means that suv is having all the properties no it's not having it is deriving it is deriving all these properties and functionalities of the super class for example as i told you earlier features and behaviors we we inherit from our parents right from the parents we inherit certain facial features physical features and some actions so here suv parent of suv is car because he is getting suv is getting features and functionalities from the parent understood so suv is called the child class or subclass and car is called super class or base class this sign this time between the suv and the list just means right yes exactly child of car class now i whoever don't have idea on super class for them a question um [Music] dot new [Music] it should start so it means like suv is getting these method from which class pair and class car right yes so understood so suv assume this is there right i will put this suv on to the top so that excuse me i will simply execute it what is an object oh monsters so three times test honda started camry started and suv also started right third one so what does it mean the child is getting all the functionality of the parent all the properties of the parent understood that is what is meant by subclass of inheritance the concept is called inheritance suv is called the child class or subclass car is called super class or face class any questions this is pretty important as well as at a rails point of view also yeah uh i have small question here okay so in that case we have to be careful on writing the methods name and variables name for for example for the subclass so now we have start and we have wheels and these things in the super class so we can't use these names in the suv class right it will be making conflict or it will overwrite it we know if we are using it it will overwrite that is the concept of subclass so if we if we write an suv diff start and print different string when i write uh i forgot the name that you no yeah but start will will take which one we'll take right that is what right this is what can you tell me which one would be a card if you i guess the other one is suv exactly so it is little overwrite little overwrite because you have a specific thing that is only child class right the concept of extension is getting all what is there in car and if at all something is changing see definitely suv that that's good space what you call the length of the height of the car everything would change the dimensions would change the four by four feature will be added these kind of stuff the wheel radius would be different right assuming so if you want to specify those things four by four etcetera there would be an extra method in here right def yeah four by four this you can't have it in a regular car right it is only for suv imagine so i can extend this suv class in order to add my extra implementation because each and every car might not have will not have a 4x4 option right yeah so i can't write the def four by four or method functionality 4x4 functionality in here i can't write it it is not advisable to write it right only in your cv or any other jeep kind of things would be there right any other type of cars you can write override it or overload it that concept in india i'll come to this after this i'll come to that concert so till now everything is clear yes i have a question on less done we have to listen what is the difference between one and two i didn't get the question can you uh okay um do you see line 25 yeah 25 yes we have here only one list done okay so actually there is two lists done and if you write another one list done what is the difference between them i don't know if you get it the tool is done another one and line 25 okay so no no no in the same line of 25 so you have suv then less than the less standing card [Music] you mean this one yes no this is not possible no you can't use that no single only we have to use single lesson so let's listen to to consider the suv a subclass from from the parent class of the super super class yes exactly so that method what you're talking about right that will that is a different method used for different classes for just for your clarification now you can have that array append for example this is an empty array i want to append value one i can do that for example i have this name i want to append muhammad so it becomes asteroid i want to append that something for example i can type in irb see now it is right so this is an empty array i want to push for example x is an empty array in the empty array i want to add 100 to it so now if you see in x there is a hundred again i want to push 200 into it this is a different one this is nothing to do with this sign at all this is only for classes specifying suv is a subclass of car class okay okay fine okay fine so we are talking about subclasses and super classes now coming back to the earlier discussion so what is the super class yeah hundred dot class gave me integer this gave me a legion i have integer dot superfly is there any super class for integer yes there is numeric it means numerically is another class again numeric numeric thought a super class yes object does object have a super class yes basic object basic object dot super class means nothing okay nothing in ruby it's like null in other languages but it simply means nothing there is no super class for basic object got it yes yes so what does it mean this means that above all at the top there is basic object sitting below it what is below basic object what is sub class of basic object object object subclass of object numbering numeric right so integer parent is numeric numeric parent is object object parent is basic object if in ruby internally it would be class integer extends what what should i write here [Music] is there is no super class for basic option got it next next let us see about floating point i directly ask for the class of it float is numeric numeric super classes this object is in yeah object object super classes basic object so in this diagram or if you take this as a diagram where this in which line float will come into picture in in this it should be after number [Music] okay it arrows you have to put arrows integer in your explorer number two arrows i will do that okay let us come to string object object super classes right so where should i put in here in this in which line should i put a string right object basically again the same question where should i put in this array also oh so understood the concept now i'll show you an inherited inheritance diagram to show you how really classes are built up related in ruby well we can just google it for class inheritance diagram diagram ruby this so let us see this this one which is where this one this one i discuss about couple of classes string object basic object there are other classes in ruby these are cn number of classes would be there in any language right so exception object basic object kernel object basic object range array object basic object hash similar right your exceptions your runtime errors everything are super class of super class of syntax error see you get a lot of syntax errors while you're working on ruby right so syntax error superclass is what according to this diagram syntax are super class the super class of syntax error this is syntax error can you see my screen syntax error exception uh no immediate super class of syntax error come on you can clearly see it is it script error or what's written yes script error exactly so super class of syntax service script error script errors super classes exception exception exception super class object object superlash is basic so from this we understand the superclass of all objects is basic object right in in ruby the superclass of all objects is basic object okay now a question pretty simple question for you i write class laptop and right this is a class what is a super class of this laptop class bhr which one object is it i don't know it's type of computers anything you you tell me anything no problem we can correct it what do you feel tell me that you can correct it anyways it's object object any other answers apart from object okay if you write a class in ruby right and don't specify the super class by default it would be a class or it would be a sub class or child of object class so same example class laptop i'll type in here emp so laptop dot super class is object if i don't specify okay let me write a class vehicle end okay and again i write class card which is a subclass of vehicle ent now i create one car honda equal to car dot new right honda is an object what is the class of it [Music] that is why we have vehicle understood yes now i ask vehicle.superclass object obviously object right because i didn't specify any super class for it right okay so understood the concept of inheritance class inheritance and ruby did everyone understand this diagram what i showed how classes are so i mean if we class it understood be already super class will be objective because every every every thing and rupees is optioned right exactly right object by default see every in ruby everything is an object so what are objects string is an object symbols an object rejects array hash range these are objects true class first class name was also object exception object this one binding method rock fiber mutates etc etc the correct way uh when you write and write a class it's a good way to specify the supercars all did not specify okay see it all depends on your requirement see if you want to write a class which is not it doesn't have a super class for example car car doesn't have a super class i don't require any super class for car car is the parent something like basic object from this i'll derive my suvs my jeep my sedan my hatchback types of cars right for example i can write class hatchback does this need a super class do you think hatchback needs a super class yes or no no no because already or i defined the in the beginning of the classes so it means i require yes i need a super class because i define the car i need a because i can access the methods and variables right yeah got it okay now another one class sedan does it require a super class yes yes it requires no [Music] i guess you know what is an excavator right excavator excavator machine this kind of thing excavator okay right so does this require a super class does this require yes what is the super plus yes it should be it should be different than car yeah if at all if at all it has a super class it should be different but for now in this example can this is this correct uh no no not at all so it doesn't need a super class so it's all your this design decisions if you need a super class you can add it otherwise leave it all right there is no hard and fast rule that every class should have a super class okay but by default any m java or any other programming language object-oriented programming language a class by default has an object class as a super class if you don't define it so do you mean a super class just for the to define the class yeah you can you can definitely classes they exactly just adjust that just for uh to define the class yes to just define a class the super class no way yeah so the exact uh uh this kind of cut right so if i didn't i if i didn't specify that class here it would not run the message inside the class right no by default it would be object class right will be object classes now uh can it come again please probably so the executor here if i run start it will not start for example if i run executors uh to start it will not start because because i didn't specify the subclass here yes yes you are right yes it will not start see excavator will not start so this will just it will be like is the rule in the ruby i need to specify the super class yes we have to specify the super class if you want to start the excavator okay or else if excavator starts with something else right it's i mean some some other way it would be like can you write the port here for exclude okay different types of excavators would be the rock excavators limestone because if i didn't specify his workplace yeah no yeah you should not yes you're right your understanding is right yeah this would be error right so where are we at this okay so we know that super class of object basic object class is nothing there is no super class or basic object then it's nil right again i'm asking question what is nil in ruby nothing right nothing or nil is an object in ruby again yes see in other languages people with programming background right null you can't relate this null to any class at all it's simply null right yes right or undefined in javascript undefined there are two things you can't relate to anything any class or any object okay but here nil is again an object okay one more tweak y equal to equals to 200 y double equal to i'm comparing 300 generally what it would return yes or no right yes false so here false is what again okay an object again an object it is not a boolean return value it is a boolean object okay okay so let me put this into some rdt written value kind of stuff so ret is boolean object so r e t dot clash would give me the class of the false object which is nothing but false object false class now i change this to 200 gives me true class got it so even your true and false or not return boolean values they are boolean objects understood yes is it confusing uh because i'm i'm i started at a very basic level if it is confusing please let me know okay so class inheritance we have seen now we will go ahead we'll discuss a few types a few types of classes what are already available in ruby this is an array this is a hash hash map or hash in ruby always you can inspect file dot class it will give you that the name of it class this one is called a range in ruby i have to specify the real existing file that would give me a file class then this is a symbol class this is a string class what is numeric.class object no super numeric.superclass is object numeric class itself the class of numeric is class please remember class of any class is class classes coming again string dot super class is object we know about this right what i'm asking string.class not superclass okay what is this this is class the class of any class is class your understanding any confusion the class of any class is class that's it super class of any classes might be a basic object or object depending upon their inheritance a class of any class is class okay for example if i can i say dot class under dot class yes i can say it's an integer right object object.class will give me the type of javascript programmers you have type of right people who are working on javascript type of string we write like this right type of string type of number kind of so this is similarly the same thing dot class will be the type of object similarly we have multiple classes math error exception argument error these number of which mostly these are internal these exception classes runtime errors are everything internal binding method proc mutex most of them are internal most of the frontend things are in ruby language what we use are these things collections and strings this one's files and io stuff okay now having discussed this still we have 12 minutes for the break so why not have a quick i mean doubts if you have any doubts quickly you can discuss in this 10 to 11 minutes what we have left for the break okay if what all you have really yeah suv double come on have anyone any chat for the slack i guess i could hear some slack mistakes i thought it was for me okay so no no questions no doubt so far so i'll continue with the next one next uh discussion can i uh so far so good okay good so now we'll start off with variables in ruby the first and foremost thing that we learn in any kind of language is variables there are around five types of variables globally local variables class variables in stands variables constants okay that these are the five types of variables we have in ruby also so when do we use each of it yes what we're going to discuss now i will simply say variables.rb on the test so how do i command a line of piece of code in line of code in ruby using so this is single length so multi line i want to for example if i execute this 2b variables dot rb i'll get errors because those are not commented if you want to comment this block of codes right equal to begin it should be the first one equal to should be the first character equal to b is to be so now it's not executed so block comment i want to comment a block piece of code block of code then equal n equal to n fine to demonstrate this first of all we will have a discussion order global variables as the name suggests global variables these variables have scope globally globally in the sense throughout the application so the whole application once you declare a global variable it will be accessible in throughout the application okay no local variables these variables have scope generally called locally locally in the sense in a method or block of code or class definitions see this is just the explanation of it with a program i'll discuss in detail how the scope works in a class class variables pretty easy these have scope only in the class defined only in the class outside the class they have no scope at all instance variables this is a tricky part but i will try to make justice to this these variables are used to create these are also global variables but they they mean this is what the meaning and scopes of each and every variable any of this you did not understand in the wording or anyone now even novice programmers did you not understand anything it's pretty difficult i know but i didn't understand two and four two and four right yeah even i don't understand initially i didn't understand when i was programming i can back this up with a small example which will start now okay yes class [Music] i say variable scopes please remember class name should always start with a capital letter and use this case capital k is for subsequent words variables scopes not a single word two words so second one should be capital as well this is a class in this i want to define global variables one more thing i forgot is global variables are declared using dollar sign local variables there is no such hard and fast rule but they can start with lowercase alphabet or underscore so does it mean lowercase does it mean local variable can start with a number can we start a variable name with the number no no way only lowercase alphabet or underscore class variables start with double attribute sign instance we must start with single athlete sign constants always start with a capital uppercase alphabet capital letter now let us declare these variables in this class i'll simply give i'll create a global variable dollar i'll simply write it as global see you can you can write it anything for example i can say steering etc etc or name [Music] or post any kind of which we generally use but for this example say i am using global underscore variable as my variable name okay which has a string value i am taking a string value it can hold any values 12 it's up to you but here in this example i am using only a string the value of the string is global variable local i'll say local underscore yeah i think it's a break time so this is a break time right uh i'm not sure all right because i think how can you feel you like it or else i'll give me a couple of minutes i'll have some water so my throat is dry now okay i'll resume it yep let's assume so everyone back can we start yes okay so i defined a local variable with the string value class variables always start to double attribute sign right so i will say class instance variables start with a single thread sign what are we left with constant right so i declared these variables in a class now i am going to access these values inside a method so i declare a method using def followed by the method name method names in ruby always start with the lowercase alphabet or an underscore just like local variable declaration and say display variables ent put string put this so um so clear anything you do not understand i'm just trying i declared these variables i am trying to print in this method so how to call this method this method come on we already discussed about it i want to execute that method called the display variables can we create a new object from that class so i create a new display so now here the question is we have the explanation defined in here so according to the explanation what we had discussed which of these variables you might think will be displayed as output who does output string global variable will it be printed onto the screen of the console this will be displayed yes local variable yes yes class variable yes yes yes we had we find it here right inside the class the instance variable ah there is no much info on this so we'll skip this problem okay instance variables we are not sure constant yes yes because it's also a global variable okay let us now execute and see ruby variable start rb i have a problem here undefined local variable or method local variable line number 28 so what is our line number 28 local variable so the problem is this does not have scope local why because you are using this one in the function only yes because it's not defined in this method right function okay if that is the case if i say put string here will it print yes yes yes let us execute yes it has printed so now here i want to print it here put this will it print here yes yes also okay for now for now i just i'll comment this out this method alone call this method and comment all of this okay simply i am executing this class okay so there are put us variables a local variable twice here and here line number 26 and line number 38 so now yes so as you can see the scope the scope of this variable he is in the class right in here not in the method that is why we are able to access that local variable in these places okay got it but not in the method but if i say local variable in here will it print yes yes it will because either i can override it or have a new it's not overriding at all it's a new local variable all together something else right this one will be printed because it's local to that method so that is what is meant by local it is local to that method okay so it will get printed i don't think it's the whole uh sorry so whole block will be printed local variable y twice again oh i'm sorry comment this out global variable is printed let me have a side by side yes it is printed local variable in the method we declare this as i declare it as open variable one yes it is local to the method it has been filtered class variable is printed why because inside the class yes it's inside the class instance variable not printed constant printed why again because it's a global variable right so it's printed but we don't have instance variable being printed so it means that instance variables do not have scoping do not i say instance methods now again a new concept what is an instance method any idea anyone who has programming knowledge anyways i'll explain the two way it's like we start install them big it will work only on the innocence of the car right so instance method are methods see what is the meaning of an instance define it somewhere else oh it is an instance of a class object is an instance of a class right so so object means instance so this is an instance right new object is an instance of this class right so i simply will declare methods which are called on an object are called instance methods instance methods did you understand or any questions methods which are called on an instance this is an object or method it's called an object or an instance of all instance methods so this is an instance method any class you can write two types of methods one is instance methods another one is class methods class methods class methods are in other languages they're also called static classes class methods or static methods so what are class methods class methods are methods which do not require an object to be called for example they are defined by the class name directly your understanding yes anyone who did not understand whole of rails is based on this concept only the whole of rails is based on instance methods and class methods you need to understand this concept i'm sorry i don't i don't think it's the whole variables types i'm so sorry but i didn't kids which one can you specify line numbers the whole program and the whole variables types like uh i get all messed up together okay i am confused so for example global variable see in ruby we can declare a global variable by using the dollar sign okay [Music] hello or looking to understand what is the global variable my voice is breaking yeah is it all right now or uh yeah yeah it's all right now it's all right so can anyone uh i mean examine interpret what what his uh issue is the whole of the program is not under he's not understand he's asking he didn't understand the difference between the five types of variables and when to use them okay yes yes i don't understand the file when i can use them little bit it's all get messed together i'm confused in this okay so global variable for example in your rails [Music] in any language you have multiple files right multiple files multiple files let us take a realistic example okay for example there is a small family okay who is residing in a three bedroom apartment okay the family consists of parents two elderly parents and two kids okay okay two kids there is a hall that is the main room what you have hall and the dining room and three bedrooms okay okay so this family bought a new refrigerator okay so where will they put this refrigerator in the kitchen in the dining room in the hall in any of the bedrooms where do you think this inflation right why should we eat in the kitchen because we are use this we are using what it is includes what is including inside it she belonged okay okay let me frame the question in a different way why shouldn't we not put the refrigerator in the parent's bedroom or kids bedroom why not [Music] right so there is no use see basically i can put this in the kitchen why because the kids want to drink water for example the refrigerator should be accessible to them the elderly parents should want to drink water or juice or anything else have something else from the refrigerator they have to access this so it should be available globally for the whole apartment right i'm a whole flat all the rooms of the house yes or no got it okay so um just means refrigerator is uh october by the way yes all the house will use it yes whole application will use it okay the whole of the application will use it cut it coming to local variable this elderly parents in order to store money they got a new iron safe iron safe you know right where we store money and jewels right gold and all where will they place it in the kitchen dining room or the bedrooms it will be in the bedroom bedroom of parents or bedroom of children parents why [Music] yes yes they don't want that to have they don't want to have access to the safe by the children right yes so it is local to only that room parents room only local variable got it this means available local it will be only used inside a particular method or a block of code only basically mostly it will be used in methods messages so we are use it in message yes understood any questions class variable see class variable this is a bit tricky example but um something like okay there are specific foods that children should not eat for example there are certain specific foods that should not eat our children should not eat those foods can be eaten by the parents and and their parents i mean grandparents and parents elderly people old people and parents okay so only the criterion is above 18 above 18 right these people can access those foods to eat children have no access to eat that food so a class of only a class of people with 18 age greater or adults can eat those foods got it those foods yes so in such cases the class variable it is used inside the class yes only that class only those types of see for example class car is there okay class variable you can have gears gears five years six gears right you can have a class variable because that will be available only for that class of cars any excavator class cannot have that right it only is the property of that class right so only then inside the class inside the methods that variable will be accessible outside the class it is not accessible okay okay okay so any uh any any other classes is not accessible not accessible no way it is accessible the same example for class and instance variable right instance variable no that is a different example i can't discuss here okay it is the usage of instance variable is entirely different once i complete this right i'll take a instance with it okay okay constant you know constant is a value that should not be changed in all the languages okay it's a constant pi for example pi is there or mathematical pi right equal to 3.14 this is a constant okay it cannot be changed so those kind of things which cannot it should not be changed are declared by capital letter constant something like this okay i want to display we'll write it with captain and we will you it will be used inside the class itself or it will be used as well see here we are discussing about object oriented program great so it is used only in everything we write we write it in class only in class yeah if it is not class for example see ruby is a scripting language also if you are not doing object oriented programming then we can write it without a class but for now for our understanding it is only in this class okay we write it only in the class okay okay next comes methods types of methods types of methods are instance methods and class methods simply methods which we call which require an object okay this method if i need to call i need to call and have to create an object and call another object right yes that is an instance method for example in your application a particular customer orders some particular ads a particular food item to his cart okay he'll click on add something like that right add so that add is an instance method inside it the code will add that food item to the cart right no i didn't get it here again can you can you make it can you say it again because i didn't get here in your application right hunga station application for example i want to order a pizza okay okay you log into the application you select that pizza and click on add right add add that item yes or no something like that yes so internally that add will be a method add add item okay something like this this now see hemanth can order add that at that time right similarly ashraf also can add that item right yes or no see i can order you can order from the application or not yes i can yes so add item who is calling that method ashraf is calling that method even is also calling that method someone else also can carry out the method so there is an object who's calling the method that is an instance method got it okay so this means yes methods which are called on an object are called by an object also i can say on by are called instance methods okay there are certain methods internally used which are not exposed to the object like external customers these are methods which are called directly by the class name these are called class methods okay any questions on this until now see simply remember there are two types of methods that can be written in any class that is instance methods other one is class method can you give us an example for a class message where can i use the class within an example see generally see incrementing values like for example switchgear for example car is there i i think the wheels can be a class variable the wheels yes exactly what is right wheels gears are no no no gears okay and so now we declare these wheels and steerings and gears everything as class variables okay so any time i want to access this we will okay we'll give a class method to access the yes we don't give an instance method because this is the property of whole class class class variable itself is a property of whole class right you are understanding yes it's common to all it's not an instance method every instance method instance variable i'll discuss so this is a property of the whole class so i should use a class method only okay so this answers your question but for other novice professionals please remember the only two methods can be written in any class that is instance methods and class methods right for now because we are just into the start of course right two methods in any class instance methods and variable class methods instance methods require an object they declared plainly by the by a lowercase alphabet or underscore but class methods are declared by class names only okay so i'll stop here and we will resume exactly after one hour so it's what's your local time in there it's 1 5 p.m right yeah we would resume at hello hi can we resume everyone is back so we can resume right i think yes okay so we were discussing about two types of methods that can be written on a class that is instance methods and the one is class methods so now i want to display see the scopes of different variables in class methods let us see so calling a class method in ruby is directly by the class name something like that so this is the method name and this is the class name this method i can call it directly by class name dot show variables whatever i define the name of the effect so i'm going to call this method it's going to execute this method anyways so what do you think will this be printed again the same question global variable will be printed yes yes local no no display no class variable yes yes because it's in the same class is different in the same class so method is the same for us instance variable we don't know much about it constant yes yes let us execute global variable since scope is global it will get printed class variable same class it will get printed instance variable got printed you see constant anyways gets printed because it's a global variable so here instance where we will have scope in class methods okay that thing you have to remember the difference is instance variables do not have scope in instance methods instance variables have scoping class methods understood that's the point of remember anyways we are not going to use instance variables like this we are not going to use we are not going to define instance variable first of all in here okay so everyone understood this example scopes and two types of methods oh yes i don't get innocence variables compared to for example java instance variables don't for now you don't compare with any language for now just remember that instance variables do not have scope in instance methods okay you mean it's not accessible it's not accessible yeah but in line 37 when we display variables the methods it didn't throw an exception so it means it's working but it was an empty variable for example yes yeah it's a nil if you want to really execute so you want to know the value of it you can already always have an inspect method on it it's nil okay so as if it was just an associated that line 37. [Music] so instance variable do not have spoken class methods at all okay sorry yeah instead of has spoken class methods not in insurance methods so this is i mean you can think of it as an uh a different behavior in ruby or someone like an interview kind of a question now generally we don't use instance variables like this at all so what are instance variables anyone can define instance variables your understanding about instance variable so that i can explain it better so this means variables it is not accessible as ahmed said from netherlands it's accessible here in all types of variable only the global variable global variable is accessible right yeah it is only the only type that's a accessible right right so the rest of them and they are not accessible every every variable will use inside their mess inside the class variable will be used in inside the class only it would be not accessible to another classes and the initial variable would be inside the methods on inside [Music] not accessible in instance methods and essence message okay yeah here i have written right these are not actually do not have they have a value of nil okay you can't access them [Music] yeah innocence variable uh it is nil and innocence missiles see first i'll explain you what are instance variables we should not generally we don't use it like this this is just an example okay i wanted to show the difference in class class methods and instance methods how the instance variable will behave so where does it have scope whether it doesn't have scope okay but general usage we don't use instance variables like this at all so where do we use instance variable first of all what are instance variables variables that are used to set at review values attribute and attribute values on an object these are instance variables let us see how to define them let's for say class person [Music] instances two people right basically now i want to print john's name okay how can i do that you make a function and plus call diff [Music] your name okay uh yeah name is what i must have to hardcore john [Music] that is wrong right so that was a instant name to this function right so here we have i my requirement is now to save the name of a particular object into its object only okay so john is an object right on the job on the john or abdul i want to set attributes of that particular person so that i can differentiate differentiate between john and abdul right in your application there are n number of customers one customer easily you are differentiating with another customer how by name by other credit birthdate etc right other criterions so basically i want to give him a name right so in order to give a name we have to use instance variables okay don't worry about the methods what i am writing but i am explaining the usage of the use case where we use instance variables so now i call execute this program to john okay now john [Music] let me see execute then i will go to the explanation undefined with the name equal person oh i missed foreign right this 20 line number has printed john [Music] something like this okay for your understand button in this channel see so what it really happened is i can show it by inspecting see i'll put a footage output string john dot inspect okay put this i don't use inspect now okay i'll comment this out okay so you can see the output can you tell me who is john and who is abdul in the output sean can anyone give it a try it should be the first one is john and second one is abdul yes right so but with my eyes i can't clearly distinguish right i just assumed by since we know programming right we assume that john might be this guy the first one and abdul might be the second one right okay so i'll use a method called inspect inspect is an object class method you know object class right super class of numeric super class of string okay so inspect now see what happens clearly i can see an instance variable with a value john abraham right in here i can see instance variable name you understood what happened so this is where we use an instance variable these are instance variable athlete sign right here what did i do simply this is a setup technically it is called a sector technically this is called so here i gave john name abraham so john dot name this method will be called we are passing john abraham to this method here so other name that is instance variable name is set to john abraham similarly abdul mohammed here john now coming to the getter john dot name name method is the getter which you are calling it is simply returning address name okay android name of john is instance variable name so we set a name on this particular object instance variable variables of instances are called instance variables this is where we use instance variables not the example what i showed in here something like this declare an instance variable this is just an example to show you the scope of instance variable in class method and distance methods but in reality we use it for declaring attributes on an object similarly i want to give an edge to these two guys john dot h equal to 44 okay [Music] will this method execute first of all no right it will not execute undefined method a is equal to per person object line number 20 because i did not define any instance variable and a setter if i gave something like is equal [Music] h something like it is if you're really confused of the method name it is return [Music] so instead of age equal to i have to use removed yeah all our instance methods because we are calling on an instance right should be get age should we get age set age sorry sorry and 29 should be set age yes right so this is the dates and it is 34 and 24. right so instance you can see here if you inspected you have inspected already name and age can be seen clearly inside the object right they are stored inside the object or not right there's our instance variable that is where we use instance variables so everyone clear about where we use the use cases where we use instance variables so this is the same background code that will whenever a user registers himself into any website or your application right the same logic would be applied to create a user object and save it to database or any data store okay so this is where we use instance variables so everyone clear about variable scopes instance variables right now a small tweak or small i can write in ruby i can write a class method even outside the class something like this uh i'll call this as show variables 2 method okay so i know local variable will not be printed so i'll remove it completely your class ends here class ends here so the class is ending here so let me see class ends here this method is inside the class or outside the class outside the class it's outside the class so i want to call this method now show variables 2. so if i execute the method what will be printed and what not i can't even think of let me know like global variable will be printed yes class variable no no while because outside the close exactly it's outside the class as she said it's outside the class instance variable instance variables have spoken class methods clearly we have written it here so they have it or not printed or not no no doesn't play off oh sorry uh i guess class variable as you said will not be printed so no scope moscow since [Music] is not getting printed no problem but instance variable is getting printed you can see here so as i start instance variables have scoping class methods so irrespective of its outside inside it doesn't matter okay this one belongs to the same class okay but the method definition is outside the class so instance variable always has spoken class methods but coming to constant we have an error it's not going to but but i told you that constant is a global variable right so it has to print so what's happening wrong the thing is we defined the constant uh inside the class if we defined the outside the class you will print right no see even if it is defined in a class it can be accessed anywhere because i told you global constants are also global variables right [Music] okay so they have global scope but here why it is not printing is because there is a special way to access this variable if you are calling it from outside a class okay that is class name followed by the constant name scope resolution constant name this one if you really open ruby on rails application if you're working on it you might know this symbol can be seen a lot of places in rails right this one yeah any one place you remember where you can where you see that symbol in the spaces yes namespaces right so i want to give a brief i mean simple uh way how where to use this okay in ruby one more thing first we had discussed when we had said that everything in ruby is an object okay now another statement these statements you have to remember if you are working on ruby another statement classes and modules are considered as constants what are classes you know these kind of classes class modules are a different entity in ruby that will take up later but these are considered as constants in ruby that is why classes start with a capital letter uppercase alphabet starting with uppercase alphabet right similarly if you're writing a module module also should start with uppercase also so these are constants so in ruby on rails let me open one okay you can see a class a model in ruby it is extending something so what is this it's another class right so active record is one part and this is another part base so which one is the class active recorder base check the frequency active record is the class that's what you say right yes but it's not base is the class so basically i want to tell you is this c is one constant sorry this is one constant this is another constant okay i am i am accessing a constant called base from this place so i am accessing a constant called base from some other place this one that is why i am using this operator got it can you repeat okay see my see i i tried to print constant so coming to the original scenario i printed i tried to print constant but it did not print syntax the problem is uninitialized constant constant error we have at line number 63 right but constant is a global variable right it has to get printed but in ruby since this constant is from a class called which class variable score yes variable scope this constant is residing in this class right so i have to follow a particular notation or syntax it means access a constant name or constant from another constant called variable scopes [Music] so that is what our classes and modules are considered as constants in ruby so since variable is a class variable scopes is a class it's a constant modules so coming to modules active record is a module so you are accessing base class or a constant called base from a module another constant okay understand it's pretty simple accessing a constant from another constant you have to put this symbol one is class one is module and otherwise variable constant right any confusion or total confused it's clear but what do you refer to when you say module for example yes okay you are asking what what is a model in ruby yes uh it's pretty early now but i'll tell you modules are something like another entity they're not classes where you write only methods in it okay and methods and classes etc some kind of in other entities okay okay for now just remember some kind of another entity anyhow we have a detailed discussion of modules and all okay um but you you have been working on ruby on this if that is case i can tell you an example in that context okay so accessing a constant from outside class or from another constant we have seen so i guess you understood about variables any questions on variables and variable scopes here for me uh can i record this this session or not no actually uh and nabin are you know already recording this session so i guess they will provide you with the recording sessions i guess was recorded because i have a lot of missed [Music] half a lot of missed part i need to review because a lot of things i don't i don't get it but should help you with the recordings because it is being recorded as far as i see the session is getting recorded so now just i mean how to write conditional statements conditional statements in ruby so i want to compare my numbers objects strings arrays etc any kind of objects i can use conditional statements which are available in ruby so what are they yeah if condition unless right anything else if else unless okay and the ordinary condition so these are statements available in 2k so let us see x is 100 i want to compare our print x print x and its value if x is 100 so i can write if if equal equal 100 something like this down two spaces is the indentation to be followed while writing statements in ruby toby this is true see brackets are optional in ruby to read not rate brackets even line separators terminators these are optional so you need not write even this like other languages okay okay the same statement can be written another way also who this x is hundred if x is 100 followed by the condition both are same so output would be a couple of times or twice okay now if friends same problem i want to say else and i want to add else condition else put this x is not hundred something like that so i change this to one zero one and say x is not hundred if and else conditions what is the mean of uh sign of uh uh uh can it come again please i'm sorry again i need to uh what is the mean of double equal again oh this okay this will compare it's a comparative operator i mean it will compare x 200 but see you see if i say it's single equal to the assignment operator right you're assigning x a value of 100 yes or no right you're giving x a value right coming to x double equal to you're comparing x is equal to 100 you are asking it right okay i'll quickly run through unless okay i want to write this statement in an opposite way using if not if not x equal to 100 then h is not 100 right else x is hundred so here i'll say x is 200 let me change this to 200 so that it would be easy for us to follow so what will be the output of this statement it will be x is 200 right it should be that x is 200 writing in an opposite way okay i can write the same not in this fashion too using the symbol not right operator basically so it would result in the same output right so either i can use not symbol or not understood so instead of writing not and not symbol i can simply use unless so what should be output of this this piece of code that's it so effectively what i'm trying to deliver or tell you is unless is nothing but if not if not that's it as simple as that okay okay similarly i can write the same kind of a shortcut statement using unless put as x is 200 if unless okay single liner now i have two variables x is 10 [Music] and z is 15z so i want to make a comparison between three three values and show which one is the highest number so i have to start off with if x greater than y and at the same time greater than z [Music] now what would be the other statement else we can write another f yes we need to write another if so it means i have to write a let's say else if y greater than on x i i cannot record for an understanding of that y greater than z then put this y is is so in our case y is greatest so let us check the validity of the program y is greatest right me here yes so any confusion on this knows you know about this ternary condition for example x greater than y greater than 10 then but why is it greater okay is okay so if i execute now this is going to happen nothing is going to happen because i have to what does this mean actually it means if y is greater than return this string so i'll simply say okay this one colon refers to else this one is else else return this this is the meaning of that particular line okay so if i want to execute nothing is going to happen as i said here nothing has been mentioned so what i can do is either say this y is greater than 10 or else equate it to a particular variable and same footage foot y is good questions so far no it's pretty straightforward right yes anyone anyone else has any doubt on this anyone has any confusion i i hope to provide news uh i was recording because zoom was was lost on connectivity with me and those recordings are safe actually with the naveen and hema so once you are done with the session right yes naveen or emma would send you the recording it would be helpful for me i lost everything even if your zoom closes right there is no problem of recording so recording is done on the cloud so now we should be able to send you the recording once it's done okay okay the session is done okay so any any questions on this it fails and a defensive kind of stuff all right okay next one is case one it is called switch case in other languages switch case i declare that system is so see you might get a doubt so actually when do we use this switch case for example your return value from a particular execution is in the limited boundaries i'm limited you know that if the output is in between these numbers or these strings etcetera okay not other than that something like http status quotes 500 400 300 this codes http codes so you know for sure it would return some kind of known code right so case one is for matching that so case x when then you want to perform i'll take a realistic example this is for example dollar amount okay your application supports five currencies for example okay it supports uh supports five currencies when dollar this is a case for dollar amount when [Music] when british found okay so you want to convert this into the user selected he's from japan so n so if it is n it should be converted into dollar amount into exchange rate rate exchange rate of n similarly corresponding to so here you know for sure that you are supporting only these things so i can use a case one instead of a felsip i can write the same statement to get this excuse me to get this functionality i can use effelsif also right i can write the same thing dollar amount is already something like this something [Music] if currency equals to en do the same step right next i'll save do them so on right so why am i not doing this but using specifically case value any ideas the question again see i can receive the same functionality using e fills so if then what is the necessity of case when i think it's a performance related because the f statements will be all executed until we reach until it reaches the one the match but in the case it will enter the specific case that match i think it's converting the condition into some symbolic yeah what is that is correct it is more like a performance it is due to performance case one is quite performant than it fails so these cases we use this case when statements are not elsewhere okay understood whenever we know for sure that only few cases to be handled by matching we may get use of case when so here when the currency is n this will be calculated suppose the user selects currency as dinar the second one else if we would return the same amount any questions on this okay so i can switch to the next topic then okay take it as a yes then next how to iterate over collections in ruby first of all what is a collection one is looping statements statements looping statements we have while until um so these are the looping circles that are available these are each and everything has a specific use for a specific use case okay for each these two especially can be used only on collections what do you mean by collections in any language containers for storing data temporary containers that are arrays and caches in ruby so in rupees we have only these two collection data structures do we have vector linked list no right by default we don't have any of those linked list stack queue nothing of that sort only we have arrays and hashes another one also called range so we have these three things working like that the array will work like the set it has a pop and portion yeah yeah so what i mean to say is see itself will work like a stack but what i meant to say is default ruby has only these two collections but all you want to write a vector you want to write a stack you want to write a queue uh you want a functionality of that right you can always write it using plain ruby you have to write your stack yourself you have to write your cue yourself it's not readily available in ruby okay everything stack you everything can be processed as an array in ruby as you told okay so these can be four and each can be used only on connections but let us see first we will see the while by example i want to just print numbers from 1 to 10. how many ways i can print it using these loops first element is of while i equal to 1 [Music] so while followed by a condition basically whatever [Music] i need to increment i write if i don't increment i i gets on i less than or equal to 10 print that i increment i i equal to i plus 1. so first time i is 1 already second time i is 2 2 less than equal to 10 yes and it continues three three less than equal to ten and so on as simple as that using while any questions on while uh can you repeat please so my i want to print see if i want to print numbers right from 1 to ten i can't simply say this one [Music] i can't go on writing this right put test three and so on it would be cumbersome right to make my job easier there are called looping statements or iterations where i can use while by a condition i can make it possible okay i is declared to be one i is less than or equal to one yes or no right i is less right so it comes into it into this put as i printing one it will print 1 here after that i equal to i plus 1 what is the value of 5 now 2 right yes 1 plus 1 is 2 2 less than or equal to 10 2 right comes into the loop prints to i is now so until the condition is satisfied it will keep on looping exactly okay so this is while similar to while is unbelievable okay go on looping it's like playing in english yeah until highest yes i is 10 so it start at nine and say until i greater than 10 now what will be the value or what will be the output one two one to ten yeah it will not run i guess exactly nothing to run right because already i is less than 10 so it's a good event greater than equal to ten so let me explain what is the range first range is a range of values this is close trend so r dot class will give me a range class range class super class is object okay so what does it include inside this range let us see r dot so it consists of values from 1 to 10 this range is holding values from 1 to 10 similarly there is another range called open range three dots now we have three dots you do the same thing in here put a psi you have only nine numbers okay the difference between two dot range and three dot range okay ranges so it's not only numbers that i can use on a range i can even say strings i have [Music] b c d up to capital z apart from that again small from a b to a z or b z something like that a b a c a d and so on b a b b b c and so on to b y since i have three dots so two dots even b z so strings and floats and numbers you can use this range okay that is how we have to use for on a range so let us see here perhaps from 1 to 10 for i any variable names i whatever you do so this is the syntax for similarly i can use this array too suppose i declare r to be an array apple okay so it has printed each value of that so any questions on this all good next we look at each one thing i forgot to tell you here yes see this up to down to each times all these are methods okay so what do you mean by methods so it means i have to call on an object right let us see first each for now we will see on an array because you don't know what is a hash map and all everything i want don't want to confuse you much so i'll declare this array once again array so array and stay here arr since each is a method i have to say a r dot each do this is a different syntax do some x xyz or whatever you want to write you can write it there um so here each is a method as i told the dot age this piece is called from do to end is called in ruby from called a block block okay it's called a block in ruby okay please this is an important uh one because most of your syntax in ruby looks like this only this pipe symbol right five symbol x is there right so x is called block variable okay so understood just the notation among that is called a block this is called a block variable so arab array dot each do this is a block so what is the value of x first time first time the value of x is above apple right and next time it's banana next time it is cherry that is why it is getting printed so first time this block variable as i told you apple banana the same statements can be written in other way a shortcut form you can call off a r r dot h um single line statement so what will be the output same output no change apple banana and siri single line statement here this is called flower braces open to flower braces is called probably closed is called block yes block so instead of this instead of do an end you are writing floor brackets do an n okay so this is called a block this whole thing sorry this thing is called a block and what is this x called block variable block variable see it's not mandatory to write x y is here it's whatever you want to write my emotions anything whatever you want you can write it here and it's also not mandatory to have only one you can write as many as you wish but rest of the things would be nil by default they don't get assigned any value by the collection okay only one variable only gets assigned a value for each iteration in this uh each method got it any questions so far can you type it if you don't mind um if i put like a boot x as a i identify excess value put value for x not in the blocker not in the block variable but inside the message did that mean and can you rewrite the code on chat and let me know i can okay what do you want to put me i mean i mean that's if uh if x i bought the value for x okay i know outside outside the loop itself right i tell him put it yes exactly so here and you want to access x here right yes yes no this is local x is not defined right you can't get that 100 so x 36 we want to iterate over the array um if it's uh if it's array it will it would give me the same array as well right see if you put an x in here now what do you think there will not be not get anything right because x is local we did not provide any x so this x is taken yeah okay this value will be taken by the block yeah it can it will take the value of axis unless you override it by writing like this something okay okay yes so i will in in any condition i can not uh i cannot put two variables inside the room right then you can put any variable multiple elements you can put multiple elements see why okay i'll say inspect and say put this okay so even if you specify this it's of no use right okay yes you can write it there is no hard and fast rule that you should not write it but it's of no use so generally we omit these kind of things when you're working with an error simply we do only this stuff okay okay yeah uh quickly we'll have a five minute break and we'll resume so this is self-explanatory up to can be used on strings and roads and um integers one up to ten do this is block block variable print x so 1 to 10 if i do the same thing but instead of that i say 10 to 1 it doesn't do anything so already yeah always remember the first one should be lesser than the latter one so generally we say 1 up to 10 right and not 10 up to 1 right yes correct similarly you can do it with twins as well alphabets something like that i'll use the short form okay transmitted from a to k correct similarly down to okay [Music] okay right as i said from 10 to 1 you can do the same thing using strings as well down to see seldom we use down to up to etc so maximum we use each and while for numeric calculations and one more thing one more times this also is so most used are times each white only these things we use but as part of our training curriculum i have to discuss what is available so i've been discussing these things next is times i want a particular operation to be executed these number of times repeated repeatedly for example i want to print hello for 10 times ten dollar times do not put this hello something like this repeated repeat those statements and number of times 10 times now a question for you i did not write a block variable right but i want to give a block variable x so what would be the value of x in here any guesses any guesses 10 10 okay any any other values will it be the index one two three four until ten exactly zero one two three four up to nine okay so kind of an index okay but it's not necessary it's just for your knowledge sake i'm explaining this so you can give a black variable times will give an enumeration to the block variable that is index 0 1 2 and so on yes so this loop do is used for some repeated checks for example until that happens something is rendered on the page you go on processing this you know for example if i try to run this right hello keeps on getting printed never ending so i have to end it by using some kind of condition right otherwise it would process forever so if i value reaches five i set it to break break statement in order to get out of the loop come out of the loop or break the loop so loop we make use of these kind of scenarios like a timeout scenarios you execute until this time out these kind of scenarios will make you look any questions so far okay then i have a question for you can i write this in javascript this is uh loud but i don't know in ruby i think it's fine the same so it is not supported so ruby doesn't support pre-increment pre-decrement post increment post-increment operators you can write i plus equal one yeah the maximum you can do is this you can do it but main thing to concentrate is ruby does not support three posts like java it doesn't have i plus plus i minus minus minus minus i i minus minus plus plus i okay are about loops now let us get into the interesting parts now about strings strings in ruby or string in ruby so how to define in australian movie first of all there is nothing such kind as a character in ruby see people coming from other languages might get a doubt saying that character care ch here data type care right so there is no data type concept there is no concept of data types in ruby so there is no specific thing something called a character and a string there is no differential differentiation between a character and this string so you can define c equal to character a [Music] hello there is no difference between these two both are strings right do you agree yes so both are strings so how to define a string using this notation you can use either single quotes or you can use double quotes for defining a string str equal to hello i can do this similarly str equal to single quotes hello i can do this as well but what is the difference between declaring it with a double quoted swing and double quotes and a single cross there is a subtle difference between the both whenever we want to interpolate values in the sense i want to substitute values in the string then single quotes would not work let us see with an example x is 10 and i want to print something like the value of x is i want to get the value of x b printed x to be printed in this string so i can say hash curly brackets and putting x in there recently es6 on es6 introduced the same kind of thing in javascript okay so this is substituting the value of x inside this string this is a string right so i'll copy this in here the output is the value of x is 10 right so this take x is substitute now let us do the same by having it as a single quote now what is the output do we have x substituted no no right so single quoted strings do not support interpolation in ruby okay never you can substitute a value into a single quota string please remember i want to get for example there is a string called hello i want to get the length of the string steer the lens str dot length right rstr dot size so how do i know that these methods exist inside a string how can i know that these i can call the length on a string anyone the question okay so str dot methods there is a method called methods of the object class where it will give me a return an array of methods that i can call on this particular object okay so it is str dot methods cut it yes but how to use it i don't know how to use it see generally we have to look at the documentation in order to get an idea of how to use the methods and what are the available methods in that particular class so what is the class of str what is the class of str ring string right string so i'm going to look at this class so since you are using ruby 2.6.0 right as far as we discussed for your project ruby 2.6 you're using right this project or the hunger station project oh you're working on two projects okay station or whatever for example you're working on 2.6.0 ruby version i have to look at documentation of ruby 2.6.0 right so what we do is class simply write last string and type in ruby 2.6 the first one this preview two this is simple so this is the documentation for your ruby 2.6 class string so if i open this class what is the parent of ruby a string class parent class or super class of string object object here it is written i can't see the screen everyone you can see it yes it's opening only the browsers probably or slow on the network or something like that because every hell everyone else could see it yeah it's actually the browser now are you opening something else no what do you see on my screen now browser yeah i am on it not loaded the browser but now it's good you think you can scroll to see if you have connection issue oh it's good okay thank you so as you can see string the parent is object and you can see these are the methods if i scroll down a bit you can see methods in here on the left hand side yes first two if you see colon colon new colon pollen tri-convert these two methods especially these are public class methods rest of them hash coming starting with the hash our instance methods i i we know what is the class method and what is an instance method right yes yes for example i want to create public class whether new how can i use this method directly by class name something like this string dot new he's asking me to provide me provide a this is class name dot new it means new is a class method right okay the other ones are instance methods so here if you look at it on the method what i used is length right somewhere if you scroll down length so length would give me returns the character length of that string that is how i am able to access or get the length of a particular string okay so now let us discuss the most important methods we are not going to discuss each and every method of the string class but the most important methods used by the string for a string manipulation or string methods most important ones the first and foremost being length that we already discussed length or size [Music] the length of it or size will return the size of the string that is 5 character length this string is tr i want to convert this or i want to i want an upper case version of this so there is a method called up case okay so what is the value of str now now after this method application after applying the method what is the valid str capital hello no it's still lower case hello okay okay please remember that it will not modify the object uh why if you want to defy it why in the sense the implementation is like that it's we are just returning the method just returns the upper case version of the string it doesn't modify it okay because you are using in the view so if you are using this one in code to view something and this happened it's already happening right yeah you can simply upcase it in the view is that you're talking about or anything else are you asking me no i'm not can you can i reach your question please oh um are you clear about it why it is not uh returning the modifying the object that's what the question was right so the default implementation in ruby up case is to return an uppercase value only not to modify the object okay so anything anything else you have any other doubts you want to modify the object permanently is that you want if you want to modify the object permanently let me know i'll write four okay similarly str dot down case it's already in down case so it's nothing to do with it or else i'll modify a bit hello world so i'll make sdr to have this hello world and str dot up case would return an update hello world what is the value of html oh hello world hello what is the value of str now should be the same as the first line exactly it should be the same so whatever operation you do something like str the swap case swap case will simply swap the cases even then sdr is still the original value only and str dot capitalize so we discuss about down and capitalize two methods in cases okay but still the value is still only this whole it is all in instance right now there is another version of these methods called str dot up case there is another method called up case band okay a case exclamatory or a case bank okay it returned hello world now str value what will be the value first here then you want to be at case value yes value so these methods are called destructive methods in ruby so let us open up the documentation and see up case up case exclamation you can see like two versions the same method up case bank up cases the contents of the string written in nil if no changes were made string ordinal returning transport okay so it will modify the object permanently okay these are called destructive methods so now the way smelly face here is this now i will try to uh and say down case now what is the valley first here missiles uh it makes a modification experiment yes modification is permanent in these kind of methods it's a technique so now what i'll do is i will swap case str dot square is now what is the output down case one right so destructive methods pretty important so this this this kind of methods is used to for passing by reference alternative in other languages it's not reference actually it's it's actually modifying the whole object altogether your object is no more having that same value i i am talking like a method like uh arrayed sort for example to return the sorted array but if there is an disruptive method for it it will sort the same array so this is something like passing by reference yes yes same yes what you said is right see once you see for example array what you're talking about there is sort method and there is also another method called sort destructive so once you use sort destructive it would sort it permanently it will sort it and make it a permanent error what is set is right so now i have str str dot reverse i want to reverse a string so again the value of str is what is the value of str hello world only so i also have a destructive method for reverse so if i use this it will be reversing hello reversing so i don't want to use that so i'll keep it as it is leave it as it is or else i have to couple of times do reverse right or something like whatever we learnt now two times okay i can do this do this bang and see it here hello if i do three times okay combination of earlier discus time method and this kind of okay but basically we are trying to discuss string reverse string method reverse okay now what are the important methods to be discussed let us see plus star is very important method plus is another important method yeah let us see so str i have this so i'll once again permanently reverse it str st star 2 twice multiplication twice it's like simple multiplication right whenever you want to use this kind of c when you're trying to log something right in ruby on rails or any other languages so if if i would do that i would try to launch something like this put this this r into hundred so it will print that line right unnecessarily i don't want to print something like this kind of stuff right you understood what i'm trying to tell you we try to print logs right i mean print debug statements like this the older way so in that case we can make use of these kind of stuff okay 105 times so str is this str plus the space in between high adding two strings adding two strings so what is the value of str it's still hello world i'm not concatenating it i'm just adding two strings together okay now earlier we asked str space i i am appending this is a method of string class clearly you can see this is a method this is nothing to do with our class definition this is a method of string class append high now your htr value is high uh all of its hello world yes he's appended like train wagons getting appended right appending this sign which is same as ben assume has been same as what japan sign it would beat you yeah left side yet couple of times a couple of times it will be same as equal it will change it will modify the string yes it will modify the string append the append is a permanent operation operation right you are pinning a string at the end right so it's a permanent operation okay okay but not addition but no addition addition this one adding yes equal to st are equal to hello world false it's not because it is hello world hi okay two forms this also returns the same but you want to override it you can override it this is very important method but i'll take it up in the next session because i don't think we'll have time to do it but i'll skip this for now square brackets method continue sorry indexing starts from zero str of zero would return the first character of the string that is h what is str of one and so on okay let me quickly change this to hello world str of 5 empty space face right space character okay str of minus one we don't have i think we will have an error the last character okay second last character minus hundred null nil yes you're right nil similarly str of 100 is also nil it is not existent right it's not existent so it is nil or nothing okay understood right that is the square brackets method see all these are methods not operators please remember these are methods and not operators it means in the string class so in the class defined class string e and d there would be methods of this kind def right you agree yes okay so these are methods and not operators fine bites bite slice capitalize i mean these are pretty simple you can do it you can have a refer have it referred by yourself char clear yes clear is one important dot clear clear will make this an empty array sorry empty string permanently so str is no more containing any characters at all it is emptied okay so i want to check whether a string is empty or not string dot empty question mark is the string empty yes please remember in this kind of methods empty question mark in ruby methods ending with a question mark always return boolean objects yes you're right boolean objects okay so if we are writing any newer methods something like def admin question mark some kind of a method where we want to return a boolean object we should write these kind of methods with the question mark these elements so we are entering some some logical to our problems okay some use [Music] so that is a database value okay is it true or false something like we have to write it as question mark okay so hello here concat st dot com cat space and world it is concatenated permanently adding the space word to hello okay that is concat method each byte and each character are important methods but mostly use each each cap so str dot each underscore byte see i will show you the documentation because i mean novice programmer should know how to look at the documentation right for that each byte how to use it you have to provide a block of code you can see here i call this as a block right it would return a string it seems so hello dot each byte this would print something like this so i have to write something equivalent to that so each byte i have to provide a block you can do it by using do and and method also or else the shortcut flower bracket method something like put as x e and d so this is the ascii character ports of each and every character mentioned in the hello world string okay so the shortcut would be str each byte this is called block of code this is called the block parameter okay so each byte will determine similarly each cap each have i'll use shortcut here onwards shortly form hello world i am iterating through each character of a string i do not mention this each underscore character in the loops that we discussed earlier if you remember right because this method is only applicable on what kind of an objects string objects right that is why we did not discuss this as part of the standard loops okay any questions so far so far so good each line if you have multiple lines you can iterate through each line something like this there is a slasher it means it's two uh lines so each line will say line if you are getting these values from a csv or a text file reading a file then it would take it into a string and iterating through that string via each line and let's go sorry what's the difference between append and insert append insert okay i'll just start now so for example html is what hello world is tr dot insert see if we want to insert at a particular position at a particular index this is insert okay so in case of append it will be the last it will be in the last one always the last right yeah okay okay and with so str dot does it end with d sorry it's a character b yeah it ends with d does it end with something else now false similarly str dot start start with capital h falls because the stair is small h okay very important method g sub very very important sub and g sub two methods are there str dot substitute or sub substitute l with some k if you see only one occurrence has been substituted the rest of them were not substituted right so that is what the functionality of substitute or sub it only replace the first occurrence now i want to replace all the and i have to use something like g sub or global sub okay now we have echo worked so what is the value of str once again should be hello world yes should be and it is hello world right so i want to make a permanent chain i can use this new method okay see there is a destructive method another very important method is include something like the substring does your string include hi true so include is a very important method so you can check whether something is being included in that particular string okay any question so far no okay so let us see insert we have seen inspect i forgot to define what is inspect in ruby inspect any method any object is converted into a string okay it shows any object in ruby in a human readable form so what do you understand by this let me show you with this small example hey there is an error with these values what is it [Music] what is the output it printed the numbers orbit array right but basically it is trying to print an array correct yes but see with my eyes i'm not able to recognize what is it so i'll say inspect see this is the second one what is printed or let me comment this out so using inspect i see with my eyes and say okay this is an array right these are not simple integers it's a entire array that is the beauty of inspect inspect shows any object in ruby in human readable form okay very important method human readable form okay that is what it does next match is a very very important method match and equal to tilde methods and take it up if time permits i don't think i can cover i can just show you the working of match and equal to partition okay i have an important method [Music] you i'll make this as capital world start something like that case 0 something like a split not an explicit splitter but make a partition by using space as the partitioning character something like that okay you partition it by space so it has partition hello space and world as well this is instead of uh substring function or the same no see there is another method str dot split see this also does the same but with a different something like split it does it uses space as a delay meter split whatever if i i need some some count of uh for three letters or split only three three three three letters you are asking about the substring method right this is definitely this we have we have substring missile yeah now this one see you can do this you can string slice 0 3 this you can do oh sorry two you want to slice it as part of it so but i want to slice each and every word of it again you have to write a piece of code for example by a space and [Music] this is what you want this is simple right so i have to extend it by using array functionality and achieve it but directly you you can't have that so collect is like map yeah map in javascript it's there is nothing i think in robbie yeah in ruby map okay so i mean collect is the same as map yes you're right connect is same as that okay and it is same as map in javascript also okay in javascript also es6 we have a map method that too does the same thing same functionality okay so can i have a small question why does they have the same two methods i mean two different names for the same functionality basically it's just an alias the underlying code is the same it's just an alias method the implementation underlying implementation is same in ruby you can only use a method by using the keyword alias you can give us other name to the same method so that people can use with the second method name they can use the same method or the original method name also you can use the same method something like that okay where are we we have seen we can even use a regular expression in order to do this for example same a space character okay i want to partition by capital letters i want to partition by [Music] e i o u this kind of this is a regular expression regular expressions i'll take kept up in the next class so what i'm going to say is we can use even for g sum so earlier we discussed the g sub rate we can even uh um okay for example i want to substitute a space with something like a star character i can do this i want to substitute all capital letters with a star character i can't do this okay something like this okay and a regular expression i can provide a regular expression basically for these methods any questions so far yes s t r dot count on how many uh l's are there three else altogether how many else and o's together five two o's and three else so it counts in that fashion it doesn't count a single kind of a letter as hello as a single unit okay please remember when you say count it will take two characters into consideration delete sdr dot delete you delete a particular character or a particular group of characters i want to delete ln l and l and o so both l and o will be deleted from the string similar to count l and o it means both so i have a destructive method as well for delete something like yeah this is what are the other important methods match is important method which i'll take it up later partition we have seen scan very important methods can [Music] i want to scan for spaces so it takes in regular expressions backslashes yes i want to scan for example there is a large string i want to scan for email addresses okay i can use the scan method i want to scan for capital letters h and w it returns an array see you can see it returns an array okay scan very important method or else without regular expression if you want to stand for i want to scan for a space i want to scan for capital h capital w nothing it now it takes as a combination of word can slice size is done scrub i've never used it let me check scrub oh invalid byte sequence okay we replace with valid bytes somewhere i mean if you get this kind of in on execution runtime executions at times in logs especially you can see these kind of characters being printed okay if you encounter such kind of things you want to scrub it means you have you can change this into a valid byte sequence using that this is the scrub method i think that we are introduced in somewhere around 2.5 i guess this method have not seen recently slice slice we have seen so it is similar to this kind of a method 0.2 will give you three characters are four to two sorry four to six will give you whatever characters similar to the slice method it's only the notation difference slice or square brackets methods will return the same values if i have a 3 it will only take in one character because three will exclude the last range rate in the range it will explain the last character index place you can see if you slice 2 0 1 2 slice 3 to 6 0 3 space is a slice s dot star t it means from s to t so yes space a space yes t this one why not the first s which one why didn't return from the first s s letter and this oh this one yeah it's a good question yeah let me check let us use the same i i think because maybe because the space is not considered asterisk yeah let me check for it so space it's not a space this is uh yes this is s letter but i mean because i think because there is no space after the s any character yeah it should take in the whole set right okay yeah what is that right from this i will suggest a modification for the documentation yeah okay if you want if you want i mean this kind of an output what we could do is [Music] backslash yes okay backslash capital is i think it's a small s for the space starting with the nest okay starting with the mesh i think it's a small s for the space so i'll remove this permanent modification and just use the original string and then dot slice yes that is what the problem was i think it's a small s for the space this is non-white space back slash capitalist this is for the white space see white space is small this one okay for non-white space non-white space okay it's capitalist okay st so what i'm trying to do is a plus would return the entire thing in non non space so this one but if you want a space as you said uh then backslash s followed by some other characters [Music] this is what you wanted right this is this is the output he has given right yeah sast let's say sast then it would be either hardcode with i or a non-white space yes forward something like this but not entirely can you open the browser again i think that there isn't there is no sa connected sas there is no connected sa in the original string oh yeah i don't know mistake making mistake yeah enter documentation mistake we can just so this is not there is no yes yes a because there is a space in between yes we can't get that output because the string itself is wrong right maybe maybe the previous slice is modified the string but still still it wouldn't match i mean if the previous line modified the string also the first one so it's a mistake it's a mistake because all of the all of the statements is modifying the string so it doesn't count yes yeah it's a very good catch actually scan we have seen size start with we have seen substitute we have seen swap case we have seen yeah converting any string into any other object type okay pretty simple and say n num number is 10 this is an integer but number is 10 this is in string so i want to add a num plus 20 it's not possible right i can't convert an integer into a string agreed so what i need to do either i have to convert i said no i have to convert this number into an integer for that num dot to underscore i would convert into an integer now i can simply add it to a 20 that is all together to make 30 right so converting a string into [Music] an integration similarly converting into a floating point to underscore it converting into a rational number converting into a complex number to underscore i yeah see complex numbers i mean it's completely math i don't think we'll use complex numbers somewhere in our academics we have learned about complex numbers right so majorly to underscore right once for f to underscore r and also two underscore s if required and any other conversions yes very important in string class and in ruby on rails also str for example str um to underscore sym very important method or str dot intern both do the same thing they convert a string into a symbol okay you have you might have seen across ruby on rails code or ruby code lot of things like this this kind of stuff right colon yeah these are symbols so why do we use symbols instead of strings so we have got four minutes so probably i can take it up now so why do we use it so this method to symbol and into a dot in turn convert a string into a symbol let us see what is the symbol symbol's definition of a symbol the simple definition of a symbol is symbols are just like strings okay and what is the difference between symbol and string let us see str okay and say str is just hello and sym i have to define using colon i'll say hi colon okay so str dot class would give me string as ym dot class will give me symbol right fine str equal to hello str dot object id in ruby any object when created would have a unique object id created in the memory okay so i'm again creating a hello object str object is the objective same no right it is different each and every time can you see that yes right so what does this mean so if i execute str equal to hello first time only one object is created on the stack right again again when i say str equal to hello and other object is being created on the stack similarly how many times i create the same object it is trying to create a new object altogether right let us see sim equal to height s dot object id again i say hi objectively how many times i do the same thing the id remains the same right yes so it means if i create the same method same object it is using the excuse means already created object agree okay so reusability so memory it's saving a lot of memory that is why in ruby on rails or ruby we make use of symbols but not strings okay understood any questions on this okay i think we are time i i think this is the closing time right if i'm not wrong yes yeah it's fine yeah yes so any doubts regarding our initial session it was a pretty long and hectic i know but this is our schedule so we had to i had to press a little bit more so any questions on this any points you don't understand feel free otherwise you can mail me directly on my personal email address at gmail.com or why are naveen acknowledge hurt also you can reach me out or else tomorrow session you can recollect whatever we have discussed any issues you find you're not able to understand able to follow in the first 10 15 minutes we could discuss and then we can go ahead with the rest of the topics sounds good yes yes thanks a lot for your time and for this session so yesterday we were discussing about strings and symbols i guess you understood the difference between a string and symbol so if so i can continue otherwise i'll repeat the same concept again do you want me to repeat symbols and swings no okay thank you so now we have left out with couple of methods from the string class majorly one being equal to tilde method and other one being match method m a t c h so this is to apply a regular expression on a particular string so regular expressions can be applied only on strings right so what is a regular expression can anyone tell me what is a regular expression regular so these are pattern matching these are used for pattern matching so what is pattern washing for example you have a string etc like this in this you want to scrape or match for email addresses so that you can get those email addresses scrape that email list and stores in store in your database and do something later on with those email addresses so you want to find a particular pattern we make use of regular expressions regular expressions can be applied only on strings okay right so in ruby also you have regular expression operands and how to use regular expressions using ruby first and make use of a online site called rubooler.com where i can validate my regular expressions in there so you can write your regular expression here your test string here and you can validate first of all what are the operands available here are the list of operands this site is pretty useful for matching one for validating your regular expression against a string okay let us start off with dot for example the hello world is my test string so i want to find out is there an i want to match a white space back slash small s is the operand for matching white space so you can see clearly that this website has matched right you can see the blue in here yes yeah so this is just for your explanation i showed this ruler.com but if you're trying to do it in ruby right for example str is hello right str equal to hello world str dot our test here equal to tilde regular expressions are always provided in between forward slashes backslash s 5 so it returned the position of the match here equal to tilde so like this i can apply a regular expression on a particular string i want to match all non white spaces a single non-white space character is denoted by backslash capital s so how many matches do we have in here how many how many matches do we have in here 10 exactly 10 matches we have okay similarly i want to add some throw in some digits in here i want to match for a single digit backslash small d so how many matches we have we have three matches because three digits i want to match for non-digits capital d so again we have around 11 matches right including the white space character so i am explaining each and every operand we could use in combination of that to get a particular pattern to match a particular pattern backslash small w is to match a word character word character means a letter a number or an underscore so if i put in an underscore in here even that would be matched letter number and underscore non-word character which is not a letter an underscore or a number this is pretty important backslash b word boundary the boundaries of words as you can see clearly these are not spaces but boundaries of the word hello three and the boundaries of the word world file okay any question so far similarly non-word boundary i guess most of you know this regular option but for others who are nervous right so for them it would be quite helping dot very important is dot a single character any single character it will match each and every single character okay start of a line backslash a start of a line does our line start with h yes there is a match does the line match start with j there is no match if there is no match you will get a red in here okay that is start of a string its not a start of a line it is start of a string similarly end of a string backslash small z 5 it is ending with 5 yes is it ending with f no now i can have i have three lines in here in the string i have three lines does the line start with so cap start of a line cap h so how many matches do we have okay two matches right there's two three lines in that we have two lines which are starting with h similarly end of a line is dollar so always if it's n right you have to write it before the opera so i'll say r ending with r what are the lines which are ending with r yes i have a match one match now i want to match for um i want to match for only capital letters okay i can use grouping using square brackets i can say a to z match only uppercase alphabets if i want to match only lowercase alphabets a to z if i want to match both any ideas a to z right i want to match even the white space just put in the white space there something else is in inside here underscore i want to match that too so sorry underscore right so group i don't want to match capital letters it means lowercase alphabets right and other other characters that are available so i can say cap a to z it means apart from a to z not a to z capital a to z not capital a to z that is your lowercase alphabets and underscore any confusion are you able to follow for newbies it is quite confusing but we'll follow with multiple examples so that it would be easy for for you so any questions so far no it's okay for us but how we will use this in fact right yeah that will use it that after discussing these operands right we'll take a huge large string and we'll apply this so that you'll understand where will it what is the use case where we use these regular expressions okay okay great okay so i want to throw in some numbers i want to match a few numbers as well so i can say 0 to 9 match only these numbers okay yes we are done with the first column second column third column we'll see in that first we'll see plus okay i have two spaces in between okay between hello and world one space and two white spaces okay so i want to match white space what is operand i i should use you can refer the table below i want to imagine white space can anyone remember backslash yes right so how many matches are there here two two right because two spaces backslash smallest will match only one single white space character not two at a time so i want to match two at a time or three at a time which are consecutive okay then i can use plus now how many matches are there it is only a single match okay probably i will take another example i want to match for is so how many matches are there roughly 12 right if i'm not wrong yeah there are 12 matches right of a thanks i want to club consecutive matches of a into a single unit a plus so you could you would see like once the server gives the response you could see it would shrink it's taking time probably the server is slow it would actually shrink actually this one too three matches let me hit once again so i said hey there are three matches so all together [Music] six plus four ten matches if i do a first in here immediately you can see they have shrunk so how many matches all together we have now three three one or more of a consecutive occurrences are treated to be one match okay so if you remove the plus again you can see it it would widen a bit yeah now i'll immediately put a plus in there it has shrunk so three matches now okay i want to match only four three specific number of patch three of three elements of a 3a number of elements are three i want to match only three a's okay so how many matches do we have here three also three only okay three three and three i want to match for a single a how many matches do i have now [Music] 10 right 10 i want to match two two two matches now how many matches do we have eight four exactly four 2 comma 3 means minimum 2 and maximum 3 so now how many methods do you have it means minimum to maximum three how many measures are there now three awesome three right this means minimum to maximum is infinite so how many measures do we have now again because 4 is also single unit okay so that is the meaning of these three now we are left out with star and question mark and r okay star means any number of characters any number any type of characters for example a star so everything following a is considered to be a match if i say a dot star following it what are the element or characters that are following it if i say n star that are following in including that match okay this is n this is representing this n dot star would represent all these characters star means anything while character right so it will match everything question mark if you look at the question mark if i simply put in a question it means like optional n might be present and might not be present if there is an n you match it if there is no and also you match it so earlier it was strict matching n n and followed by wild character all the characters any characters but now we are saying optional it might be an or anything else so it matched even a something like optional might be present might not move or a better example would be your user names uh usernames for a website for example [Music] john david is it a valid username for yes right yes john.david no no we can we can right gmail allows us to do it right yes john david is this valid yes also yes so for example we are trying to we want our username our website to accept only these three types apart from that it should not accept any kind of a hyphen or some special characters kind of okay only an underscore dot and this type so it can accept see here it can accept any number of factor any type of characters like numbers etc but not in the middle okay this is our criteria now let us write a regular expression simple regular expression for that let us start off with john david here so john david i mean some people prefer to use johnny these kind of stuff right so it's up to them so and say [Music] backslash w are better i will use grouping [Music] okay followed by an underscore i have to put in plus any number of occurrences followed by the same thing with a plus sign yeah this is valid this is a regular expression matching this so it is valid now let us say i change this to dot is this valid in our website john.david is valid right we will be allowing that kind of user ids right to register so this is valid but the regular expression seems to be wrong so i have to fix the regular expressions so i'll say either you can use an underscore or a dot fine understood or a orb any confusion here till here no it's all me now john david is also valid uh right it's a valid username so it takes in that so it might be present this one but let me check plus one month i just got a call yeah so this is the one and it also can say an optional underscore or a dot or nothing so there here we can say it is an optional might be present might not be present okay now let us see captures we will see at the last we are only left out with captures that we will see once we discuss a few examples on regular expressions so that everyone would be understanding about regular expressions i'll just search for v plus 15 and see on this ruby that we want programming language to be so i'll copy couple of lines from this in here and i'll paste it here so here is a large string so what i want to do is i want to find out all r u b y i want to match all r u b y strings in this string words in the strings so what should i write in here regular expression your understanding yes write ruby slash w right yes that's it okay but now i'll change the requirement a bit i want to match all the words which are starting with capital r and ending with a y you can see in your laptops also you can open this website and refer to this operand okay starting with r and ending with y any one slash w for example uh underscore [Music] uh it is not right the bracket in chat so what uh it should be bracket yes i i voted in a chat is that right oh so you mean to say this yes but it's matching all level v r u b y s and everything i want only starting with r and ending with y okay for this you can put in a capital r first because it starts with r and ending with y so backslash s plus non-white space and y okay ruby but there is a slight problem here what i'll do is i'll add an i in the front but this is not satisfying my requirement right i want which is a word which is starting with capital r but this is not starting with capital r instead it is starting with capital i right so this regular expression is wrong okay so we have to modify this regular expressions in expression in order to get what we really require that is starting with r and ending with y so we have to use in in these cases always remember using word boundaries okay okay similarly for example i remove this i now match and y it is not ending with y right it is ending with h so here also we have to specify a word boundary then only we can clearly say word is starting with this character and match match words which is starting with this character and ending with this character okay understood absolutely so i have a ruby in here i'll make this a smaller lower case of it's not matching i want to match even that so if you can clearly see regular expression there is another box in here i can say i it means ignore case okay it will ignore the case now another regular expression i want to match all the words which are 10 or greater in length you understood the question the words whose length is greater equal to 10 or greater than 10. and start with which letter any letter you want it to be started with or only the number of characters number of characters so number of characters should be equal to 10 or greater than 10. so question is words that have length equal to 2 or greater than 10 this is the question okay any ideas of okay let us start off so i have a specific boundary right amy so all i can say a word curly braces yes curly braces then yeah so it's taking some time let us write a regular expression and okay got it okay words with 10 or greater you can see only 10 specifically 10 fail again see here d g i want specifically 10 in length again the same concept okay it should be a word right if i want to greater than 10 i can add out the [Music] currently [Music] curly bracket plus just mean more more than 10 no oh more than a diamond good question i never tried it let us check so specific kind of plus kind of then we can go ahead with uh best backslash w plus but this would any number want to say something like so specifically uh a to z kind of a sorry and now using a plus no only if you use brackets it will only consider that many numbers okay so it will not no it work work plus one or four that's right see anywhere in the example you don't find that kind of stuff right it will not work okay okay you can use a comma to get more or you can a specific length boundary 10 to 12 characters then you can use comma 12. then i want to get all the words which are starting within vowel so it's pretty simple just put in the group and a e i o u okay then what backslash yes capitalist plus so that you consider each and every and the word boundary as well okay but if you see it is keeping off eiffel right how to make sure ignore case so that it will match both capital and lowercase alphabets understood any questions so far no it sounds good okay now i want to get uh these kind of words right i want to get all the special characters available in this string anyone special character yeah special character in this string something like this codes commas pull stops apos if available so these brackets not these not these so spaces no i don't want to consider space only special characters these are special characters right commas full stops quotation marks square brackets etc right this is what i wanted so not a to z capital letters not lowercase alphabets not zero to nine i mean digits and not space character okay can we add the dot and slash d backslash d uh you mean instead of this instead of zero to nine i mean instead of zero to nine you want to add a backside yes also we can instead of a to z we can insert a dot dot is any single character right it could match any character yeah yeah okay right so you can at most do backwards w yeah okay i want to match uh for example you want don't want a full stop to be selected you can put in a dot see the pull stop is not selected right it's not matched you're getting the point can we only insert a dot and instead of the w and d and s can you remove the w and d and yes i don't want only to select a dot you want me to select a dot i want to select everything instead of a dot oh you still have a dot okay isn't this yeah isn't this should be every character i mean why did he sell why did it select only the dot i think we had to add the backslash before the dot work fine yeah it's already added now i mean it will work see in grouping if you write the character it alone consider that as a operand for especially for dot okay so you can escape it by backslash it doesn't matter even if you do not escape why there is no difference it's implemented like there are terms the implementation itself is like that see generally if you are writing for example generally if you want to escape a dot right we want to capture a dot or we want to match a dot then we would specifically escape that dot in order to say that don't use it as an operand use it as a dot literal dot right okay uh for example japan okay japan i want to match japan dot okay so i will say j x s plus and a dot so i say dot so this dot for if you see it is taking it as a single character right yeah because it matched even comma here so i have to escape it don't take it as a single character operand i'm talking about this any single character right yeah don't take it as that but instead you take it as a physical dot at the full stop okay so then we have to escape right but if it is in the square brackets it will be considered like a dot yeah but see yeah it is considered as a dot but you can even apply the escape sequence it doesn't make any difference even if you give it don't do it doesn't make any difference i give it i don't give it this happens only in the square brackets right okay okay so now what else can we find can we find this kind of stuff yeah i want to find out match this kind of numbers or characters order let's put in a couple of brackets whatever in bracket square brackets i think a backslash square bracket yes and and yeah right yeah as simple as that any other things you want to know in this we want to extract or match if we if we want to exclude all the things in square brackets what to do i mean we need to select everything instead of the selected currently oh apart from the square bracket stuff yeah we should put this in square brackets and add the knot yeah that's it yeah okay so um any any rejects we will will do it after that we will would be inside the bracket that's cool bracket you can specify this means that we need to use the square bracket every time to specify the not rejects all about your requirement see one you like if you want that kind of requirement then you can square brackets otherwise no if you want apart from that i mean not then you can use square brackets okay so without knot it can we can use it without bracket and with not we needed to use it with bracket right exactly that's that's clear thank you anything else we want to find out in the string okay okay let us take a small data example for discussing extraction okay now since we have learnt regular expressions in here we'll use this same string and try to get the output in ruby okay so till they i mean we have just seen regular expressions into it but how to apply methods in order to get the required stuff while programming how to get that let us declare str equal to