Masterclass | Ruby Programming in 1 video | Beginners Ruby HandsOn Crash Course Interview FAQs |

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello and Welcome to the complete crash course on ruby programming I am Raghav and you can find all my work on my website AutomationStepByStep.com in this video we are going to learn everything from scratch and we will start with a hello world program in Ruby I will show you how you can copy my project and then we will look at comments variables we will look at how to get user input from console strings operators arrays and hashes in Ruby and then we will move to conditional statements ternary statement case statement for loop while and do while loops we will look at until and unless keywords break and next redo and retry keywords we will see how to create methods and classes and objects in Ruby so lets get started and here I will go to repl.it this is a online editor I am going to use you can go here and create a free account I have created my free account and here you can see I already have some Repls I already have this here but I am going to create a new Ripl or a new folder I will click here you can also go from here I will click here and select the language so today we are going to learn Ruby I will select Ruby and I will name this as Ruby crash course and say create repl so this will create a repl and we can start creating our Ruby programs here so it is loading and here once this is loaded you can go to the settings and can select the layout side by side or stacked I will say side by side then the theme light or dark I will go with the dark theme you can select the font size for my case it is large indentation and all the settings you can do from here and then here you can write your code and can also create new files see the files are here then here you can go to version controlling packages database settings we just want to go to the files here here I can write my code and here I can see the output it is also a shell I will show you this later so now I can start typing here so let's start with our hello world program so in Ruby I can say puts Puts is to print anything on the console and if I say hello world and if I run this you can see the output here it says hello world now I can also say p and say something here So p is the short form for puts it will be the same thing you can see it prints this here now I can also save print hello again and if I run it now you can see it is printing hello again as well however after printing it did not move the cursor to the next line so this is the difference between puts and print puts Moves the cursor to the next line after printing and print does not move it if you want you can also create a new file here if you want to have easy categorisation you can do like this and now if I say something here puts hello if I now run this you will see it is still running our main.rb and if you want to run this file will have to load it here I will say load and I will say hello dot RB and now if you go here and run it will now Run your files so it is saying hello there so you can do that but for now let me just keep on this file so this is a hello world program now you can also get all my Ruby project so if you want this Ruby project you can just go to this link repl.it Ruby crash course so this is my link and if you go to your browser and just hate this link you will get my entire project Ruby project from here and even if we do not create account on repl.it you can still copy my entire project from here and if you want you can do a forking so here you can see all my project is here and you can get it from here so let me just leave this and come back and now let us go to comments so how do you create comments now if I go here and if I have to comment everything, i will select everything and press CTRL and Forward slash / on my keyboard if you are on mac press CMD and forward slash / and it will comment out everything and it will put a hash # here so for a single line comments you put hash for a single line comment and if you want a multiline comment so if I want to put everything inside comments I can say =begin and then =end so everything inside this block will be a comment so this is how you can do commenting let us come to variables so we can give any variable name let us Say I Say name equals and I can give value so this is a variable and then I can say puts or print name and if I run this you will see it is printing this here and then I can have a numerical variable or a numerical value in a variable I can say 123 or I can have a Boolean value I can say condition = true and then I can print everything I can say puts and I can also say like this name coma num coma condition and if I now run this it will print everything here so this is how you can use all these variables and store values inside your variables now here if you want to print something I can say hello and if you want to refer this variable name you will have to say hash and in { give the variable name and if I now run this it will say hello Raghav so you can do interpolation or refer variables like this in Ruby how to get input from user console so let us say let me comment everything I will press control and / on my keyboard and now lets say I want to get something from the user in that case I will say a gets dot chomp this is the function we use and before this I will save print enter your name and then I will say gets dot chomp and this will get the user input from the console and let me put this in a variable called name and then I can print hello and then I can say hash name if I now run this on the console it is saying enter your name I will say Raghav and hit enter It is saying hello Raghav now just in case you want to get a integer or want to convert this into an integer if I say print enter your ID in that case I will create a variable and again say gets dot chomp but in this case I will convert it to integer so i will say dot to underscore i this is convert to integer and then I can print hello name and say your ID is hash ID and if I now run this it says enter your name I say my name is Raghav and then it says enter your ID I say 10 it says hello Raghav your ID is 10 so this is how you can get a user input from console let us learn about Strings so here I will comment this control / and now I can say I can give any variable name str1 and say this is a string I can also use a ' instead of " this will also work fine this is another string and I can say puts str1 and puts str2 and if I now run this it will run and say there is some issue let me check ok so this is str2 I forgot here let me run it now again and now this is saying this is a string and this is another thing so this is working fine now we have a lot of string operations in Ruby how to create multi line string so if I have to create a multiline string I can say STR 3 = this inside "and saying this is a and I will present enter multiline again press enter string Now You See I have just pressed enter and if I now say puts str3 and run this let us see the output so it is printing it as it is so I just have to put the string in a different Line or what I can do is I can say STR 3 = percentage and / and / and inside this forward slashes I can write my string I can say this is my string and if I want to print it I will say puts STR let me say STR4 here we have already used str3 here now if I run this you can see it is also printing my multiline string like this I can do multiline string interpolation is how do you refer variables in string and this we have already seen here so you just say hash and the variable name inside { to do interpolation or referring of variables let us see how can I get length and can do upcase downcase I have a string say STR5 and I say it is hello now I can say puts STR5 dot size OR I can say puts STR5 dot length if I run this let us see the output so it is printing the size or length of my string I will comment this earlier part and now if I have to do upcase or downcase I can say puts STR5 dot upcase OR STR5 dot downcase if I run this is it will convert it to uppercase or lowercase and you can see it has converted it like this and then I can also say puts STR5 dot reverse if I want to reverse my string I can use the dot reverse function and you can see it has reversed I can also reverse after doing upcase so downcase I am saying upcase and then reverse so I will say reverse and if I now run this it is converting it to an uppercase and then reversing my string then I can say include so I can say STR5 dot include and if I give something let us say hello and if I run this it will say true that means this is a part of the string if I use something else like with capital at H Hello and run this now it is saying false that means this is not part of this string and then I can concatenate my strength so for example I want I have multiple things I can say hello and then I can just give space and again give the next part how are you you and if I run this you will see it will concatenate and then print it on the console so let me very quickly check this I will clear my console and run it again and you can see it is saying hello how are you the other ways I can also put a plus in between and this will also work if I run this now it is still printing the same thing or I can say hello dot concat and then I can give the next part I can say how are you and I will run this and you will see it is now printing it as it is so this is how you can concatenate strings then you can also freeze your strings let us say I will say STR6 = hi and I will print this and then I will modify this string I will say STR6 = STR6 and then I will append something hi again and then I will print it again STR6 let us see the output I will just comment all this control / or cmd / on my keyboard and yes I'm here now I will run this and let us see what happens so it has modified the string Hi again and now if I say STR6 dot freeze so I have freezed my string and if I now try to modify it again let us see what happens now if I run this it says there is an exception you cannot modify a frozen string so if you want that nobody should be able to modify your setting you can use dot freeze operation and then if you want to compare two strings you can use double equals to for example I have this string I can say = = ABC and if I run this it will print true let me comment out this one and then run again and you will see here true if I say I can also say abc dot equal and then I can say something and if I run this now it will say false so these are two ways how you can compare strings so all this we have learnt you can also use this length and you can get something out of your string using all these operations let me just copy this from here and paste it here suppose we have a string called message and then you can just say puts message hello and if it is a part of the sting it will printed so here it is printing it then you can use these index positions to print some particular part of your settings if I say message 0 it is printing h if I say message 0 to 4 it is printing HAL that is the first 4 characters I can use a range here I am saying 0 to 20 inside square brackets and it is printing the 0 to 20 characters I can say zero until the length of the string so it is now printing the entire setting I can go from reverse that is from the back side of the string using minus So if I say minus 4 it is printing the fourth character from the back which is S here this one and then I can also print the length as we have seen earlier so this are Sting operations Let us move to operators and I will comment everything now let us move to operators now in Ruby we have all these mathematical operators and then we also have these comparison operators so there are multiple operators let me very quickly copy this code to save some time and I will just copy it here so you can do addition using + operator - subtraction using minus - operator multiplication using the star * and then this is divide / and this is modular % so this will give you the result as the remainder of this division so when you use percentage it will show you remainder of the division and this is exponential ** so if you use this double star this is 2 raise to the power 3 and then this is comparison == if 2 is equals to 3 this will check not equals this will check greater than less than greater than equal to and less than equal to Let me run everything and check and you can see it is now printing all this so you can get this code and try it out let us now move to ternary operator so here let us say I say A = 10 B = 20 and then if I say C = I can say give a condition A is greater than B and then I'll give a ? and then I say A and coln B so what does this mean is in ternary operator we give an expression or condition here and then a question mark if this condition is true this will be the output of this will get stored in C if the condition is false after colon : whatever the expression we have will get executed so condition if true this will get executed if false this will get executed and then finally I will say puts greater number is and I will say C so let us see I will run this and check and it says given number is 20 so this is how you can use a ternary operator Let us move to Arrays and before I do that I will have to comment everything and now Let us move to arrays now in Ruby you can create an array I can say give any variable name like array1 equals and I can give these square brackets so this will create an empty array or I can also se array2 = array dot new both of these ways create array and in this case it has created an empty array if I try to check the length or size of these arrays I will say array1 dot size and I can say puts array2 dot size or length this will be the same I run this and you can see it is printing 0 0 so that means they are of zero size or empty I can just give us size here so if I say bracket 10 and now if I run this you can see the second array is of size 10 and for the first array if I want I can directly give the values here so now if I run this you will see first array is of size 5 and the second array is of size 10 so this is how you can create arrays now we have all these functions I can get something access element at a particular index using dot at function so I can say puts aray 1 dot and I can say at and then I can say in the square brackets [] I can say what index I want let you say 2 and if I run this you can see here there is some issue let me very quickly check and I have to use a circular bracket () and not a square bracket [] I will run this sorry for that and check again and it is saying empty because there is nothing at this position I will say 2 and if I run this you can see it will print 3 that means at index position 2 we have 3 because the index position starts from 0 so this is index position 0 this is 1 and this is 2 and then I can use fetch now if you say this you can see the difference here I will say puts array1 dot fetch and I will again say give some position here are you can see the difference when I say at 10 it gave me empty and did not give me any error but when I say fetch at 10th index it will give me an error that is is array index outside the array bounds if you want to get this error then you can use fetch let me very quickly comment this out and then I can use first and last I can say puts array1 dot first and puts array1 dot last it will print the first and last element of the array and you can see one and five here then I can say take n and IT returns the first n elements of the array example if I say puts array1 dot take and I say 2 here and run you will see it is printing the first n elements that is the first two elements from this array then I can say push to add the items to the end let me very quickly just use all this to save some time and I will have to remove this and I will say array1 dot push and I will say 6 array1 and I will say 7 array1 dot let us say 0 array1 dot at this position insert 4 so what does all this mean when I say push it will add item to the end of the array when I use this symbol to append it will again add to the end and if you want to add at the beginning or the start you can say unshift and when you want to add any object or element at a particular location you can use insert and give the index position and then the object so now after all this when I say puts array1 you will see the difference so I say this and now if I run let us see I will have to stop and clear my console and run again and check ok yes there was some error I can go back to my earlier project and go to arrays so this is what I have here just to make sure I if I making any mistake I will check this so let me try to print this and check puts or print and the same thing I will print for here and here and here so I will run it again and check and yes this time looks like this is running but it is all printing in the same line so I will add a puts in between so it changes to the next line and then I can check the the errors and exceptions so let me do this and clear the console and run it so here this is all fine you can see it has added 6 here than It has added 7 then it added 0 which is what we did here unshift and zero I will try to delete this puts I will clean and say I do not want actually want this let me just say puts and run and let us see and yes this is all working fine so we have done all these operations and then we can say drop now drop will return elements after n elements have been dropped so if I say a print array1 dot drop and I will say 2 and if I run it now so you can see it has dropped the first two elements and then it is it is not printing our array then we can say Pop Pop will remove it from the end the element from the end shift will remove element from the start delete a particular element will remove the specific item for example if I say here I say array1 dot delete and i will say delete 3 and now if I run this it has removed 3 and now if you print your array there will be no 3 there so let me say print array 1 and run this I should be using print here and puts will just move it to the next line I will run this again and check and now you can see 3 is removed from the array then we can use unique it will remove any duplicate items in your array if you see my array now if I run this and check my array you can see 4 here is multiple times so I will say array 1 dot unique and run it now you can see 4 is removed and now we only have all the unique values in our array so this is all about Array Now let's go to hashes now array store information or values within the numerical index and we have seen that we can use the numerical index to access our arrays now Hashes store information using key value pair and you can create a hash like this so for example let me just comment all this and now I can say hash 1 = I can give a { and this is a empty hash and now if I want I can add the key and value I can say name and I can say equals and this Arrow and then I can say give value and then I can also say here I can give a coma and add more values I can say subject and I can say here automation and then I can again give a , and in the next line I will say topic and then I can give here the topic name so this is the hash I can say puts hash one dot size and if I now run this you can see it is saying 3 now the other way of creating a hash is I can copy this and I will say hash 2 you can give any name and now here instead of this I can use a colon and so this is also a valid way of creating hashes and I can also print its size I can say hash 2 size and if I run this this is printing 3 again now if I have to access anything in my Hashes I can just use the square brackets and use the key for example I can say puts hash one and here I will say name and if I run this it is getting Raghav here or I can say puts hash 2 and if I say topic and if I now run this now here it is not printing anything so I will choose a colon and I think outside let me check like this and run this and now I am getting Ruby here so this is how you can use and access hashes and you can also use a loop to get all the values from your hashmap or hashes so let's say I will say hash 1 dot each and I say do and in this pipe symbol I say key value and then I say puts inside the loop and then I say hash key and hash value so it will get the Key and value out of the hash let me copy it again here and say hash 1 and now if I run this and now it is running and printing the keys and the values this was about hashes Let us not go to if else conditions in Ruby and in Ruby you can create IF ELSE conditions and this is the syntax if the condition and the code and then end so let us create a if else condition I will comment this and now I will say if 2 is greater than 1 then I want to print 2 is Greater and then I will say end if I now run this it is saying 2 is Greater or if I say I will say age = 18 and in the if condition I say If age is greater than 18 then print you are eligible to drive now if I run this because this condition now will not be true let us see the output I will clear everything stop clear and then again you can now see it is not printing anything because this condition becomes false and it comes out of the if statement so to have something after it becomes false I can say else and I can say puts you are not eligible to drive and now if I run this here you will see it is printing you are not eligible to drive now you can also say have a multiple else if so I can say elsif and then I can say else if age is less than or equal to 18 then print this and finally I say else print enter a valid input so now if I say age is20 and run it will say you are eligible to drive if it's less than 18 it will say you are not eligible to drive and then if you have any invalid input it will go to the else block so this is if else and if statements ternary we have already seen it is a kind of a short form of if else where we give expression and then question mark if this expression is true this is executed if it is false this is executed we have already seen this Let us goto case let us say I have a array called days and I can have values like ok or I can have values like 1 2 3 4 5 6 and 7 and then I say case and I will say when one then I will have to say case day so it switches to day and then I say when one I will say puts today is is Monday and then I say when two then say today is Tuesday and so on and if it does not match anything than I can say else puts enter valid input so I can also get this from a user I can say print entered a number and then I will get it into a variable called day using gets dot chomp dot to integer and let's see what happens now I will say run and it says ok the issue was this was my mistake I also have to end this I did not end it now I will run this and now it say enter number I say one and today is Monday I run it again I say two it says today is Tuesday and if I give something else here then it says enter a valid input so this is how you can use case statements and here in other programming languages it is kind of switch case where switch comes instead of case and then instead of when we have cases this is the syntax in Ruby let us go and see for loop now I will comment this in Ruby I can say for i in 1 to 10 this is a range and then I will say puts i and say end and if I run this it will print 1 to 10 I can also use a array I can say for i in this array 12345 and I will say puts i and say end and this is the same thing if I run this it will print all these elements of the array so this is how you can use for loop then coming to while and do while loop so while loop executes while condition is true so I can say while condition and then do and then end so I can say here let us say while say X = 1 and I say while X is less than 10 do I will say print X and I will also make X increment by 1 I can say x = x + 1 or I can also se X + = 1 which is the same thing and then I will end the while loop I will clear and run and you can see it painting 1 to 9 this is because we set less than 10 if I say less than equal to 10 and then again this will now print from 1 to 10 now in a do while loop it is similar to while loop the only difference is that it will execute at least once so let us see this code this is how we use a do while loop and i will comment this so we say loop do and this is our code I am saying enter a number greater than 10 and I am getting a input from user and if I say If number is greater than 10 then I'm saying break that is coming out of the loop so if I run this it says Enter a number greater than 10 if I say 6 which is less than 10 it is still going in the loop until I give a number which is greater than 10 it will not come out now I say 20 and now it will come out of the loop so this is how you can use a do while loop in Ruby now going to until and unless conditions so until will execute until the given condition evaluates to true and let me show you this code here here I say i = 1 and I say until i = = 10 it will keep on printing i and I am also incrementing I with one and here if I run this you will see until the condition is true it will keep on running the loop and then unless executes the code until or unless the condition is false if the condition is true code specified in the else clause is executed so let me show you unless as well here I am saying x = 3 and saying unless x is less than 5 x is greater or equal to 5 else put this so if this condition is false only then it will execute this if I run this you can see it says x is less than 5 which is printing this because this condition is true so you can use unless like this then we have break and next we use break and next in loops break is used to come out of the loop and next is used to skip that particular iteration of the loop let me show you an example if I say x = 1 and I say while x is less than or equals 10 and here I am adding a condition if x becomes equals to 5 then break out of the loop and if I run this and check you will see it is going until four and as soon as X becomes 5 it is coming out of the loop however if I want to skip a particular iteration of a loop I can use next so if I use this I will comment this and if I say I am using a for loop I am saying for i in 1 to 10 and as i becomes 5 I am saying next so it will skip this I will run and check you can see it is printing 1 2 3 4 and 5 is not getting printed because this is keeping that iteration and going to the rest of the loop this is how and when you can use break and next Let us see Redo and Retry Redo is to repeat the current iteration of the loop let us see an example I will say x equals one and while x is less than 5 I am printing x and also incrementing x to 1 and then I am saying Redo if x = = 5 that means when this condition becomes true the current iteration of the loop will get executed let us see if I don't to this what happens I will not use this or comment this out and run my code you can see here it is printing 1 to 4 it is not printing 5 because we are saying x less than 5 if I run with the Redo I say redo if x = = 5 now you will see here it is also printing 5 because as this condition becomes true it is Re executive that particular iteration of the loop and then retry is to repeat the whole loop iteration from start and retry statement only works inside begin and rescue block let me show you I will copy this and paste it here I am saying for i in 1 to 10 and using a begin I am raising an exception if i become 10 and in the rescue section I am saying retry so what is will do it when i becomes 10 it will retry the iteration and it will keep on doing this it will be kind of a infinite loop if I try to run this you will see it will be infinite and I will have to stop it manually so you can see it is it will keep on running until I stop it so this is redo and retry let us now go to methods in Ruby so we use methods or functions so that we have a modular code and it is a block of code that returns a value or you may also have a method without returning any value and we use methods so that we can run the same code again and again and we have reusability and in Ruby we use the keyword def so we say deaf and give a method name and then I can put any code let us say hello from method and then I say end and if I run this now nothing happens because we are actually not calling this method to execute method we have to call this method so I will say hello and now if I run you will see it is saying hello from method it is a good practice to give brackets here it can be empty arguments but this is how we create a method so it is printing hello from method now I can also give some arguments of parameters let us say user and then I will say I will prefer this variable user here and now if I run this you will see there is an exception because now this method is accepting one argument and we are not providing any argument while we are calling the method so I have to provide the argument or a value or a parameter here I say Raghav and if i now run this it is saying hello Raghav here I can have have multiple parameters as well I can say user and ID and here I will say your ID is and I will say hash and ID I will provide two parameters and I will say run and you will see here it says hello Raghav your ID is 10 so this is how you can create methods in Ruby and now I can call this with multiple parameters so this is now reusable I can say John and say 20 and if I now run this hello Raghav your ID is10 and hello John your id is 20 so now this is reusable code Let us see classes and objects in Ruby if you have to create a class in Ruby you just have to use a Keyword called class and then you can give it any name let us say i say car you have to say end to end the class inside the class you can have any variables and methods let us say I have a method I say get name and then inside this I will say puts car name is now here let us say I have to call this method so I will have to create an object of the class and to create object of the class you can give any variable like car1 this will be the instance of this object or the instance of the class and we are calling referring this instance or object with this variable car1 and then I say car dot new so this is now created an object of the car and we are calling it car1 and with this I can access the variables and methods now I can access car1 dot get name and if I now run this you will see there is a issue now I should not be using the curly brackets here that was a mistake and now if I use this object object reference car1 I can access the methods of this class I can say get name and if I now run this it says car name is star star star this is what we are printing here now I can create some variables in the class I can say @ name this is a instance variable that we create using the @ symbol and a class variable we can create by double @@ symbol now I can say this @ name now if I say hash and inside { } I refer this and if I run this now you can see it is saying car name is empty because we have not yet set this variable so I can use another function I can say set name and inside this I will get a argument called name or carname inside this function at name equals to whatever I am getting here and before I call this I will call set name function and here I can give the name let us say toyota and now if I run this you can see it is saying card name is Toyota so this is how you can set the variables like this and now this variable can be used anywhere now sometimes we want to force the user to give some arguments whenever creating object of a class in that case I can use a function called initialise Now initialise is kind of a constructor if you know about Java or other languages where we force a user to give some arguments whenever creating objects so let us say I have name and I have top speed and then in the initialize function I take name model and top speed and then inside the function I say all these variables I say this equals to name this will get equal to whatever model we provide here and this will store the top speed that we provide here and now if you see I can just remove this set name and if i now try to run my program you will see there is a exception because now it says this initialise needs three arguments and we have not given any argument so we need to give the arguments as soon as we create the object of the class that is here so here I have to give the object I will say here I will give have to give the arguments I will say Mercedes and let us say 2020 and let us say top speed hundred and now if I I run this you will see it is now there is some error let me very quickly check it says undefined local variable yeah there is some issue this should be top speed so there was a typo I will now clear and then run again and now you can see it says card name is Mercedes and similarly you can access model top speed now also if you want if you go to shell you can just say Ruby and give the name of your Ruby file and run this from here it will still run your Ruby program from here as well so you can also use shell from here and then you can get all my course all my Ruby course with this URL if you go to your browser and just open this URL I will also have this in the description of this video so you can get from description and then if you need any more help you can always go to my website that is automation step by step dotcom and you will find all these courses here under the program section I hope this was very useful for you and if you have any issues let me know never stop learning and I will see you soon thank you for watching
Info
Channel: Automation Step by Step
Views: 10,265
Rating: undefined out of 5
Keywords: learn ruby in one video, ruby programming for beginners, ruby beginner tutorial, how to use ruby programming, ruby for beginners, ruby crash course, learn ruby, ruby programming handson training
Id: xyDoP5a_dvo
Channel Id: undefined
Length: 49min 35sec (2975 seconds)
Published: Tue Jan 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.