Ruby Programming Language – Full Course

Ruby Programming Language – Full Course
Spread the love

in this course i’m going to be teaching  you everything you need to know to get  started writing programs in ruby now  ruby is an extremely popular programming  language and it’s also the programming  language behind one of the most popular  web development frameworks called ruby  on rails so if you’re somebody who’s  looking to get into ruby on rails or you  just want to learn more about ruby  you’ve come to the right place in this  course i’m going to be showing you  everything you need to know to get  started so we’re going to start off with  the basics like installing ruby getting  ruby set up with the text editor and  executing your first ruby program and  we’re going to look at you know some of  the bare basics things like variables  we’re going to look at dealing with  different types of data storing data  we’re going to learn things like if  statements and loops and then we’re  going to get into more advanced stuff so  we’re going to talk about the ins and  outs of object oriented programming  we’re going to talk about things like  classes and objects and all throughout  this course we’re going to be using real  world examples so i’m going to be  showing you guys different you know mini  applications that you can build we’re  going to build a couple different games  we’ll build like a calculator we can do  all sorts of stuff so not only are we  going to be learning ruby but i’m going  to be showing you guys how to apply it  in a bunch of different situations so  i’m excited for you guys to come along  in this course i’m covering just about  every major topic in ruby so by the end  of the course you should have a pretty  solid understanding of what this  language is about what it can do and  what you can do with  it this tutorial i’m going to show you  how to install ruby on windows it’s  actually a pretty easy process  essentially all we have to do is use a  ruby installer that we can download from  like ruby’s official website and it’ll  basically walk us through all the  installation instructions so i’m over  here on this website it’s  rubyinstaller.org  forward slash downloads and this has  basically a windows downloader that we  can use so you’ll see down here there’s  a bunch of different options i’m going  to be installing  the latest version at the current time  which is ruby 2.4  just a quick disclaimer the instructions  in this video aren’t going to work for  ruby versions below 2.4 so below 2.4  there’s actually a different way that  you can install it but after 2.4 and  above then you can follow these  instructions so i’m just going to click  this and it should start downloading  when the download has finished now all  we have to do is run that program so i’m  going to go over to my downloads folder  and you’ll see we have this ruby  installer let’s double click that and it  should open up a window that we can work  with  all right so i’m just going to click  through all the options on this window  i’ll accept the license  and now ruby should start installing on  our computer  all right when the installer finishes  running all we have to do now is install  one more thing so you’ll see over here  there’s a check box that says run ridk  install you want to make sure this is  checked and i’m just going to click  finish so this should actually open up  another window here it’s just it says  ruby installer 2. and you’ll see over  here that there’s three options so the  first one says msys2 base installation  second one says system update and then  here it says msys2 mingw development  toolchain just for the purposes of being  thorough we’re going to go ahead and  install msys2 and the mingw development  tool chain  and i’ve found doing this it’s best to  just enter all these options in order  sometimes when you enter just three then  you get an error because certain things  aren’t updated so i’m gonna go ahead and  enter in one and then after that’s done  two and then three  and if we do that then everything should  be able to be downloaded correctly so  i’m just gonna click one and we’ll go  ahead and run this  when that’s done running then we can  just click two  and we’ll run that  and this is gonna go ahead and update  everything that we just installed  and finally i just want to click this  third option so i’m going to type in  three and we’ll run that all right once  you’ve run all three of those installers  then we’re officially done with  installing ruby on our computer the last  thing i want to do is just check to make  sure that everything got installed  correctly so i’m going to go down here  and inside of my search bar i’m just  going to type in cmd so i’m just going  to type cmd  and this option for the command prompt  should come up so you just want to click  that the command prompt is basically  just a way that we can interact with the  computer by giving it text commands and  we can go inside here to check to see  what version of ruby we have installed  so essentially what i want to do is just  type in ruby hyphen  v  and when i click enter this should tell  me the version of ruby that i currently  have on my computer as long as you’re  getting a version with the ruby hyphen v  command then you have ruby installed on  your computer and you’re ready to start  writing some awesome programs  in this tutorial i’m going to talk to  you about installing ruby on os x now  one of the great things about os x is it  actually comes pre-installed with ruby  so basically you don’t actually need to  install it you should just already have  it but i just wanted to talk about in  this video like how we can check to make  sure that we have ruby and then i’ll  also talk a little bit about updating  the version of ruby that you have on  your computer so first thing i want to  do is come up here to the search bar and  i’m just going to type in  terminal  basically this will open up the terminal  the terminal is essentially just a  program that allows us to interact with  the computer but using text commands so  inside the terminal i just want to check  to make sure that everything is  installed with ruby so i’m just going to  type out  ruby  hyphen v  and this should spit out a version  number of ruby in my case i have ruby  version 2.4.2  so as long as you’re getting a ruby  version number that’s getting printed  out onto the screen then you’re good to  go and for the most part if you’re  running os x you should have some  version of ruby installed on your  computer  if you’d like to update this version  though for example if you have maybe an  outdated version of ruby i would  recommend using a program called the  ruby version manager i’m not going to  get too into using the ruby version  manager here but i just wanted to bring  it up so that if you are running an  outdated version of ruby then you’ll  know kind of what to look for in order  to update it so something called the  ruby version manager and it’s basically  a program that you can use to update the  current version of ruby that you have on  your computer so if you’re trying to  update your current version of ruby then  use the ruby version manager but  otherwise you can basically just use  whatever ruby version that you have  installed on os x in order to follow  along with this course  in this tutorial i’m going to talk to  you guys about getting a text editor for  ruby and we’re also going to look at how  we can write our first ruby program and  run our ruby programs from the text  editor that we download now one of the  cool things about ruby is you can  basically write ruby code in any text  editor you want so you could use  something simple like notepad or text  edit or you could use like a dedicated  environment that was built just for  writing rwby  in our purposes for this course i’m  going to be using a special text editor  called adam and this is basically a text  editor but it’s been designed to support  ruby and so it’s going to be an awesome  environment where we can write our ruby  code so i’m going to show you guys how  to install adam we’re going to talk  about setting it up and getting it ready  to go but just a quick disclaimer like  you don’t have to use adam if you don’t  want that’s what i’m going to be using  for this course but like i said you can  basically use any text editor that you  want to write your ruby programs so  let’s go ahead and install adam i’m  going to come down here to my web  browser  and up here in the url bar i just want  to type in  adam.io and this is the official website  for the atom  text editor what this should do is it  should bring you to a page for your  operating system so in my case i’m on a  mac so it gives me this download for mac  option if you’re on a windows machine  then it’ll give you a download for  windows option i’m just going to click  this and we’ll go ahead and download  adam when adam has finished downloading  i’m just going to go into my downloads  folder and i’ll just go ahead and double  click on this adam mac.zip file and if  you’re on windows you might have to go  through an installer process if you’re  on mac though now you basically have  adam over here so you just move it into  your applications folder and you’ll be  able to start using it  so i already have adam installed on my  computer so i’m going to go ahead and  open it up  and once we have adam open there’s a  bunch of different stuff that we can do  one thing you might want to do when you  first get into adam is go down here into  preferences or settings  and over here you can configure a bunch  of stuff so you’ll see over here on the  side we have an option for like editor  you can also configure like a theme so  you know you can change like what the  text editor is going to look like um you  can do a bunch of stuff over here just  to kind of configure adam to your liking  in order to be running ruby programs on  here we’re actually going to have to  install one thing which is a package  so over here on the settings page i’m  just gonna go over here to this install  button  and inside of here we wanna search for a  program it’s just gonna be called  adam hyphen runner  and  essentially what this plugin is going to  allow us to do is it’s going to allow us  to run our ruby scripts or ruby programs  from inside atom it’s going to make it  really easy for us to do this so you’ll  see right here it’s just adam runner and  i’ve actually already installed it but  over here there should be an install  button just like you see down here so  click the install button on adam runner  and once that’s installed then we have  everything we need to start programming  ruby from inside here  so  now that we have adam runner installed  i’m going to show you guys how to set up  your first ruby file so the first thing  i’m going to do is come over here to  file and i’m actually going to add a  project folder so i’m just going to  click add project folder  and i’m just going to add my documents  folder because this is the folder that  i’m going to be working with throughout  the course  and i’ll just click open as you’ll see  over here now we have this little like  file explorer window so that can  actually be pretty useful just so we can  see like what the different files are  that we’re working with  so inside this documents folder i’m  actually just going to click right click  and i’m going to say new file  and i’m going to go ahead and create a  new file and i’m just going to call this  file draft.rb  so whenever we’re creating a ruby file  we want to use this dot rb extension and  that’s basically going to tell our  computer that this is going to be a ruby  program and you can name it whatever you  want i’m just naming it draft so i’m  going to click enter and now we have our  first ruby file up and running so what i  want to do now is just write a simple  line of ruby code inside of here we’ll  test it out we’ll make sure everything’s  working and that way we know we have our  environment set up to go for the rest of  the course so over here i’m just going  to type  print  and i’m going to type in open and close  quotation marks and i’m just going to  type hello world  and i’m just going to go ahead and save  that so just print this out and  basically what this does is it just  prints a line of text out onto the  screen  so now what we want to do is use that  atom runner plug-in so remember we  installed that atom runner plug-in  before and now i can actually use that  to execute this ruby code so i’m just  going to click control  and r so control r  and you’ll see this is going to go ahead  and run our program so down here at the  bottom of the screen i actually have  this little output window and you’ll see  down here that it’s printed out hello  world so it’s gone ahead and printed out  hello world onto the screen so  as long as that’s working as long as the  control r was able to run that atom  runner package and we were able to  execute the ruby code then everything  set up we’re ready to start going in  this course we’re ready to start writing  some awesome ruby code  in this tutorial i’m going to talk to  you guys about the basics of ruby  programs so we’re going to write a very  simple ruby program we’re going to talk  about how those programs get executed  the order in which the instructions in  those programs get executed we’re also  going to talk about different ways that  we can print things out onto the screen  this is going to be a pretty awesome  tutorial i’m really excited first thing  i want to show you guys is how to print  something out onto the screen so you’ll  notice over here i have my little atom  runner output window and in the last  tutorial we talked about setting up this  basic environment so what i want to do  is show you guys how to print something  out onto this little console window over  here so all we have to do to print  something out is just type print  a space  and then if you want to print out like  plain text we can just make an open and  close quotation marks and we can put any  text we want so i could put like draft  academy here  and now when i run this program by  clicking control r  you’ll see that draft academy gets  printed out onto the screen over here  there’s also another way that we can  print something out onto the screen so  in addition to just saying print i could  also say  puts  and puts basically works the same way as  prints it’s going to take whatever we  put inside of these quotation marks and  it’s going to print it out onto the  screen so for example i can type my name  like mike  and now this is going to get printed out  onto the screen over here now you’ll  notice that draft academy and mic are  getting printed out right next to each  other and this actually brings me to the  first thing i want to talk about which  is the difference between print  and puts  so you’ll notice here i’m using this  print command i’m printing out draft  academy and then right below it i’m  printing out mic using this puts command  and when i use this print command mic  just gets printed out right next to  draft academy as you can see over here  but if i was to take this puts command  and move it up here right above draft  academy so now the program’s going to  execute puts mic before it executes  draft academy what you’ll notice is that  draft academy gets printed out on a new  line that’s because whenever you use  this puts command it’s going to print  out whatever you put over here and it’s  also going to print out a new line  afterwards which basically means that  when you use puts you can print things  out on different lines when you use  print however everything gets printed  out on the same line so if i wanted to  print out two things right next to each  other for example i could print out  something over here like  is cool  now these are gonna get printed out  right next to each other so you can see  it says draft academy is cool but if i  was to do the same thing with puts for  example if i did another puts down here  and i said is cool  because i’m using this puts up here this  is actually going to get printed out  onto a new line so you’ll see down here  that’s exactly what happened so  that’s the two different ways that we  can print something out onto the screen  using prints and using puts again puts  will print out whatever you specify and  then it’ll print out a new line  print will just print out whatever you  specify no new line so that’s the basic  difference between those two so now what  i want to do is just show you how we can  use these print statements to draw a  little shape out onto the screen so over  here i’m just going to say puts and i’m  going to make an open and close  quotation mark and i’m actually going to  copy this a couple times so we’ll paste  this like a few times down below here i  want to show you guys how we can just  draw a basic like triangle shape  so i’m going to start with a forward  slash down here and i’ll make another  forward slash another one up here  and another one there and now i’m just  going to make vertical bars going all  the way down  and i’m going to make some underscores  down here  and now we have a basic little triangle  right here so you can see i’m using this  put statement and individually on each  one of these lines i’m printing out some  text so now if i was to go over here and  run my program you’ll notice that we’re  printing out this triangle and it looks  pretty good so this is sort of the  basics of drawing something out onto the  screen i also want to talk to you guys  about how these programs get executed so  when we run our ruby program or our ruby  script basically what happens is ruby’s  going to go through and it’s going to  look at each line individually inside of  this script it’s going to start with the  first line here so it’s going to say  okay the first instruction that the user  wants me to do is print this line of  text out onto the screen so ruby’s going  to look at this first instruction it’s  going to execute it and then once it’s  done with this instruction it’s going to  move on to the next instruction so it’s  moving on to the next line  and it’s going to go ahead and execute  this instruction  and then when it’s done with this one  it’s going to move on to the next line  and the next line etc so ruby is looking  through each instruction that we give it  inside of our program and an instruction  in ruby is just something like this it’s  like this puts or that print that we saw  before there’s tons of these different  instructions that we can give ruby but  the point is is that ruby is going to  execute them in the order that we write  them so ruby’s going to start with this  line move on to this line etc so if i  was to take this bottom line here  and move it up here to the top  now it’s going to execute this line of  code first so you’ll see over here we  get this like funky looking shape so  that’s sort of like the bare basics of  how ruby is working we’re giving it sets  of instructions right i’m just writing  out a bunch of different instructions  and ruby’s going to execute them in  order and it’s going to do essentially  whatever we tell it to do so right now  all we’re doing is telling ruby to print  something out onto the screen but as we  learn more about ruby and as we go  forward and we learn more instructions  that we can give it we can actually make  programs to do just about anything so as  long as you can specify specific enough  instructions for the computer you can do  anything inside of a programming  language and that’s kind of like the  core concept that i want to give you  guys today which is how these programs  are structured and how to write  essentially a basic program that prints  a shape out onto the screen  in this tutorial i want to talk to you  guys about using variables in ruby  now in ruby you’re going to be dealing  with a lot of different data and  generally anytime you write a program  there’s going to be all sorts of data  that you want to maintain and manage and  a lot of times when you’re dealing with  large amounts of data it can be  difficult to manage it can be difficult  to keep track of it so in ruby we have a  special container where we can store  data values called a variable and a  variable is pretty awesome because we  can actually take a piece of data or a  piece of information in our ruby  programs we can store it inside of a  variable and then whenever we want to  use that piece of data or access it or  modify it we can just refer to its  variable container and you’ll see in  this tutorial why variables can be  really useful so let’s go ahead and jump  in down here i have a basic ruby program  written out essentially all it does is  it prints out a story it says there once  was a man named george he was 70 years  old he really liked the name george but  didn’t like being 70. and you’ll see i’m  just using this puts instruction over  here  and  here it’s just printing out the story  onto the screen so we’re essentially  just printing out everything that’s over  here onto the screen over here so this  is a pretty awesome program right it  works it’s completely valid  but let’s say that i wanted to go inside  of my little story here and start  modifying some of the information right  let’s say that i wanted to change the  character’s name so maybe i don’t like  the name george and i want to change his  name to john  so i’m gonna have to go through and i  found that first place where the  character’s name was and i have to keep  searching through and then okay here’s  another place where we have the  character’s name so i’m gonna change it  so now i’ve officially changed the  character’s name in the story let’s say  that i’m reading over the story again  and i’m thinking to myself hmm i think  we can make the character a little bit  younger  so instead of 70 why don’t we make him  35 right so again i’m gonna have to look  through this entire program  all right found the first 70 so i’ll  change this to a 35. i have to keep  looking through and okay here’s the  other 70 so we’ll change this to 35. all  right so now i’ve updated the name and  the age but here’s the problem  when i wanted to change the character’s  name and the character’s age i had to  manually go into my program into this  story and modify each value so every  place where the character’s name was  mentioned i had to update it to the new  name every place where the character’s  age was mentioned i had to go in and  update it and this is kind of a problem  right this is a situation where we have  two pieces of data the character’s name  and the character’s age and we’re trying  to keep track of that information right  but imagine that instead of just having  a story that was four lines i had a  story that was hundreds of lines long  right and they mentioned the character’s  name you know hundreds of times if i  wanted to then go through and change my  character’s name and my story it would  be a real drag right i would have to  look through hundreds of lines of code  in order to do that same goes for the  age and this is actually where variables  come in so this is a perfect example of  a time where we have two pieces of  information the name and the age and we  want to be able to keep track of them  and maintain them a lot better than we  currently are so what i can actually do  is i can create a container and i could  put the character’s name inside of its  own container i could put the  character’s age inside of its own  container and then when i want to access  that information and use it i can just  access that container and that container  is called a variable so i’m going to  show you guys how we can use variables  to seriously increase the usability of  this program so up here  up above these puts lines i’m actually  going to create a variable  and whenever we create a variable we  need to give ruby some information the  first piece of information we need to  give ruby is the name of the variable  that we want to create so generally when  we create a container to put information  in inside of our programs we like to  give it a descriptive name right  generally you want to give the container  a name that will identify what  information is inside of it so the first  thing i’m going to go to over here is  type in the name of the variable that i  want to create so i’m going to create  one called  character  name  and generally in ruby if you’re creating  a variable you want to give it a  descriptive name and if there’s going to  be multiple words like character name  you want to separate them with an  underscore  so the next thing i have to do is tell  ruby what i want to store  inside of this variable so i can just  say character name and i can use this  equal sign and i can set it equal to a  value so i can say character name equals  and i’m just going to say john because  this is going to be the new character’s  name  so now i’ve officially created a  variable called the character name  the next thing we can do is create  another variable to store the  character’s age so i’m going to do the  same thing i’m just going to say  character age and i’m going to set this  equal to  35. so now we have two variables both of  which are storing values  so down here in my program what i can  actually do instead of just typing out  the character’s name like this you know  manually i can actually just refer to  the variable that is storing the  character’s name so over here instead of  saying there once was a man named john  i can actually just get rid of this and  outside of these quotation marks  i’m actually going to make a plus sign  and now i’m going to type out the name  of the variable that i want to put in  here so i’m just going to say  character underscore name essentially  what i’m doing is i’m saying i want to  type out all of this text over here  plus i want to type out the value that’s  inside of the character’s name so i want  to print out the value that’s inside  this character name variable there’s one  more thing we have to do anytime you’re  using a string of text like this and  you’re also using a variable name you  need to surround this whole thing with  parentheses so i’m going to put a  parentheses over there  and i’m going to put a parentheses over  here and so now  we’ll actually be able to use this  program so let’s go ahead and we’re  going to run this program and now you’ll  see that over here it still says there  once was a man named john he was 35  years old etc but you’ll notice that we  didn’t have to  manually print like type out the word  john  all we had to do was refer to the actual  variable name character name so i just  referred to this variable and it was  able to insert the value that was stored  inside that variable inside of our print  statement so i could basically do this  same thing for  this guy down here so i’m just going to  copy this and i’m going to paste this in  down here so now i’m adding in the  character name where john was in that  other spot  so over here we’re also going to have to  put another parentheses  and i’m going to do the same thing for  the character’s age so over here  once again we’ll just surround this with  parentheses  and now i’m just gonna  close off both of these so i have two  separate little strings of text inside  of quotation marks and i’m gonna put a  plus sign i’m gonna type in the variable  name  character age and then i’m going to put  another plus sign essentially what i’m  doing is i’m telling ruby that i want to  print this out plus i want to print out  the value inside of the character age  variable plus i want to print this out  over here so that’s essentially what  this is doing and we can do the same  thing down here so and again we’re going  to need to surround this with  parentheses so  once we’ve done this now we’ve actually  set up our program to use all of these  variables so every place where we  mentioned the character’s age we  replaced it with the character age  variable every place where we mentioned  the character’s name we replace it with  the character name variable so when i  run this program now you’ll see over  here we get exactly the same output as  we did before we’re printing out the  same exact story  the only difference is now that we’re  using variables our program is a lot  better so for example if i wanted to  change the character’s name inside of my  story instead of having to go through  and manually change it in every single  spot where we mentioned it  i can actually come up here and just  modify it so i could change the  character’s name to mic  and now without having to modify  anything else in my entire program  the character’s name is going to be  updated to mike so you can see now it’s  using the name mike and that’s really  why variables are powerful because they  allow us to organize and they allow us  to keep track of the information and the  data  inside of our programs a lot better the  other thing you can do is you can  actually modify the value of variables  so let’s say halfway through my story i  wanted the character’s name to change i  can actually come down here  and i’m just going to put this line of  code right after these first two and  right before these second two i could  change  the value of a variable so i could say  character name  and i can just set it equal to something  else so i can set it equal to tom for  example so now when i go and run my  program  you’ll see that halfway through the  story  the character’s name changes so over  here it’s mike and then halfway through  it changes to tom you can update and  modify the values of these variables  throughout your program and that can be  extremely useful so that’s sort of the  basics of variables and these can be  really useful there’s a lot more that we  can talk about so for example  over here i’m just storing text  information right i’m storing like the  text mic i’m storing the text 35 down  here we’re using all this text but in  ruby we can actually represent and we  can store a bunch of different types of  data inside of our variables and that’s  what i’m going to be talking about in  the next tutorial we’re going to be  talking about data types so there’s all  different types of data types we can  sort like text data numbers true false  values a bunch of different stuff so  stick around for the next tutorial and  we’ll talk about that  in this tutorial i want to talk to you  about data types in ruby a data type is  essentially a type of data that we can  represent and use inside of our ruby  programs and by default ruby is going to  support a bunch of different data types  so these are basically just  types of information that we can use in  our programs and i want to talk to you  guys about the different types of data  that we can use and we’re going to look  at how we can store those different  types of data inside of variables  so down here i’m actually going to  create a couple different variables and  we’re going to store some different  types of information inside of those  variables and the first and probably  most basic type of data that we can  represent on our ruby programs is called  a string and a string is basically just  plain text so anytime we want to  represent plain text in our ruby  programs we can use a string so i could  just make a string called like name  and i could set it equal to mic so this  is a good example of a string it’s  basically a variable and we’re storing a  string value inside of it so the string  is just like any plain text right you  could also make another one  occupation we could set it equal to like  programmer whatever you can store any  like information that you’d want to  store as plain text inside of your  programs as a string we could also store  numbers so for example i could store a  whole number like if i wanted to specify  someone’s age i could say age is like 75  right so someone might be 75 years old  we can represent numbers like this  inside of our ruby programs and you’ll  notice that with a number i didn’t need  to use these quotation marks i just had  to type out the number that i wanted to  type out you can also use decimal  numbers so this right here 75 is what  would be referred to as an integer and  an integer is basically just a counting  number like 2 3 4 5 6 7 basically a  number like a whole number a decimal  number is different because the decimal  has decimal points after it and a lot of  times especially in ruby we’ll refer to  these numbers as floating point numbers  basically just means that they’re  decimal numbers so if i wanted to  specify a floating point number i could  say like gpa we can set it equal to like  3.2 or you can set it equal to 3.285 for  whatever like you can set it equal to  you know whatever decimal point you want  to represent inside of ruby so using  integers and using floats you can  represent different types of numbers you  can also make these negative so i can  make this like a negative 75 or a  negative 3.2 and ruby’s going to be just  fine with that  in addition to storing numbers we can  also store something called a boolean  and a boolean is basically a true false  data type so a lot of times in programs  we’re going to want to represent true or  false data and this might not be  something that you’re super used to  doing in the real world but in  programming when we’re specifying types  of data a lot of data is going to fall  into like the true or false category for  example if i created a variable called  is male  this variable could tell us whether or  not someone is a male in which case it’s  going to be like a true or false value  right they’re either a male or they’re  not it only has two possible values so i  could set this equal to true because i’m  a guy you could also create one you know  it could be like is tall and this  boolean variable would tell us whether  or not someone’s tall right so if you’re  not tall then it would be false so a lot  of information in our programs can be  represented with one of two values  either true or false we can also store  one more type of information which is  called nil and the nil data type  basically means that it doesn’t have a  value so for example if i created a  variable called like flaws  and i set it equal to nil  basically what this means is this flaws  variable doesn’t have a value so we can  go out of our way to say that something  is nil like to say that it has no value  so these are the basic data types in  ruby and there’s actually like a couple  other like more obscure data types that  we could use but for the most part these  are the data types that you’re going to  be using as a beginner to the ruby  programming language so we can store and  represent information in either text  form with a string  number form with an integer which is a  whole number or a floating point number  which is like a decimal number  or we can use true or false values and  we can also use nil which would mean no  value so these are sort of the basics  and as we go forward in this ruby course  we’re going to be looking at all sorts  of ways that we can work with this type  of data  in this tutorial i want to talk to you  guys about working with strings in ruby  now strings are one of the most common  data types in ruby and for good reason a  lot of times in our programs we’re going  to want to represent and store and work  with plain text data that’s essentially  what a string is  so in this tutorial i want to walk you  guys through the basics of using strings  we’re going to look at some different  things we can do with strings we’re  going to also look at different things  called methods which we can use on  string so a method is essentially just a  little block of code that we can call  and it will either modify our string or  give us information about our string so  it can be pretty awesome first thing i  want to do is just show you guys  basically how to print out a string so i  could say  puts  and over here to create my string i just  have to put it inside of open and closed  quotation marks so anything i put inside  these quotation marks is going to be  considered a string so i can just type  out like  draft academy and now this is going to  get printed out over here onto the  screen works pretty well there’s a  couple things we can do inside of these  strings one thing you might be wondering  is how can i print out a quotation mark  so for example this string is surrounded  by quotation marks we use the quotation  marks to tell ruby where the string  starts and ends but what if i wanted to  use quotation marks inside of this  string  if i just try to put one right here  you’ll see that it messes everything up  and it ends the string in order to use a  quotation mark i can put a backslash  and then put the quotation mark and this  is basically going to tell ruby hey i  want to literally enter in the character  quotation mark i don’t want to use it to  end off my string so now i should be  able to  print this out over here you’ll see  we’re printing out a quotation mark you  can also use something similar to print  out a new line so let’s say that i  wanted to print out draft and then on a  new line print out academy i could type  a backslash n  and i’m actually going to get rid of  this space and now you’ll see that this  is going to print out draft academy on  two separate lines so that can be pretty  useful another thing we can do is we can  store strings inside of variables so if  i didn’t want to just type this out like  this i could put it inside of a variable  inside of a container and it’ll be a  little bit easier for me to work with so  why don’t we create a variable called  phrase  and i’m just going to set it equal to  draft academy now if we wanted to print  this out all i have to do is come down  here and just type in the name of the  variable that i want to print out so now  we’ll just be printing out phrase  and you’ll see we’re still printing out  draft academy so using these variables  can be pretty useful and when we’re  working with strings we can actually use  things called string methods or  sometimes you’ll hear people refer to  them as string functions essentially  what these are are little blocks of code  and we can call these blocks of code and  they’ll go off and they’ll either modify  our string so they’ll change it in some  way shape or form or they’ll give us  information about our strings these can  be really useful and there’s just a few  that i want to show you and you’ll kind  of get the hang of how to use these  whenever we’re going to use one of these  methods we just want to type out either  the name of the variable that’s storing  the string or just the string itself and  then i want to type out dot and now i  want to type in the name of the method  or function that we want to access so  i’m going to show you guys a couple that  are pretty useful i found them in them  to be pretty useful the first is called  up case so you’re just going to type out  up case and then an open and closed  parentheses and actually this open and  close parentheses in a lot of situations  is going to be optional but i’m just  going to include it just to be super  correct so when i type phrase.upcase and  we run this program you’ll see now it  takes our string it takes our phrase and  converts it entirely into uppercase  letters you can also use another one  called down case so instead of saying up  case we’ll just say down case and this  is going to convert it down to all  lowercase letters so this can be a  pretty useful little function there’s  also another one called strip so if i  had a string that had a bunch of leading  and trailing spaces  right i wouldn’t necessarily want to  just print this out so if i print this  out onto my screen  you’ll see that we get it printed out  all weird right there’s these you know  spaces in front there’s these spaces  after you can use a method called strip  so i’m just going to type out  phrase.strip  and now when i run my program all of  that leading and trailing white space  gets deleted so a lot of cases when  you’re dealing with a variable you might  not know if it has leading and trailing  white space so you can use this strip  method to make sure that all that  disappears we can also use these methods  to find out information about our  strings for example instead of saying  strip i could say phrase.length  and this is going to tell me how many  characters are inside of this string so  you see over here we get 24 and actually  let me get rid of all this white space  so now we should get a smaller number  15. draft academy has 15 characters in  it that’s including any of the spaces  that we put inside of it in addition to  figuring out how many characters are in  a string i can also figure out if  certain text shows up in my strings so  for example i could say phrase dot  include so i’m going to say include and  now i’m going to type a question mark  and then i’m going to type a space and  now i’m going to type a string i’m going  to type another string  basically what we’re saying is  we’re asking this include method whether  or not this phrase  includes the string that we’re going to  put over here so for example if i put  academy right here this is going to  return a true or a false value telling  us whether or not the word academy shows  up inside of our phrase so over here we  should get a true value because academy  does show up if i was to type out like  academies though so for example this  isn’t inside of our phrase it’s going to  give me a false value so that’s a really  good way to figure out if a certain  string or a certain phrase or a certain  character  shows up in the string that you’re  working with we could also access  individual characters inside of these  strings so for example let’s say that i  wanted to just figure out what the first  character in this string was so maybe i  was just given this variable phrase and  i don’t know what’s inside of it and i  wanted to figure out what the first  character is i can type out the string  and then i can make an open enclosed  square bracket  and inside of these square brackets i  can put the index  of the character that i want to access  so if i wanted to access this g  i can actually put a zero inside of here  so now you’ll see this is just going to  print out that g  if i wanted to access this i i could put  a 1 in here  and this is going to print out the i if  i wanted to access this a for example i  could it’s going to be 0 1 2 3. so i’m  going to put a 3 in here and now we’ll  be able to access that a  so if you haven’t caught on already the  way that we assign index positions to  strings in ruby is starting with zero so  if i was going to give these characters  index positions i would say that g is at  index position zero  i is at index position one r two a three  f four other f5 e6 etc so  whenever we wanna access the first  character in a string we have to access  it using index position zero and this is  kind of like a staple of using strings  in ruby the first character is always at  index position zero so essentially ruby  starts counting at zero so anytime we  want to  use this little method right here where  we’re just passing it a number you  always want to start with zero as the  first character so let’s do a little  experiment let’s say i wanted to access  this capital a it’s going to be zero one  two three four five six seven eight so i  put an eight in here now we’re going to  print out that capital a so play around  with these different string indexes you  know this is obviously not super  difficult to understand but you want to  get used to starting indexes at zero as  you start programming you can also print  out a range of characters so let’s say i  wanted to figure out what the first  three characters inside of this string  were well i can say zero i can type a  comma  and i can basically specify a range so i  can say i want to print out or i want to  get access to the characters from  position index position zero up to  another index position so we can say  zero let’s say we want the first three i  can say zero  up to zero one two three  and this is actually going to give me  the first three characters so it’s going  to give me zero  one  and two and it’s actually not going to  give me that third index position  character so i should just get gir here  you can see that’s exactly what we get  so when we specify a range down here we  start the range at the first index  position 0 and we end it at 3 but we  don’t include the character at index  position 3. so we didn’t include this a  so that’s basically how we can grab like  characters in a specific range inside of  the string you can also use another  method and i’m just going to type phrase  dot index  and basically what this is going to do  is it’s going to tell us what position a  specific character in our string  starts at so for example i could say  phrase dot index and i could type in  like a capital g and this should give me  a zero because zero is where our capital  g is at if i typed in like a capital a  here this should give me the index  position where capital a shows up inside  of our string so that’s going to be an  8. you can also type in just a normal  string here so i could say like ffe and  this will tell me where ffe starts  inside of my string  and it starts at index position four so  zero one two three four this index  method is actually pretty useful so  these string methods can be extremely  useful when you’re working with strings  and they basically just allow you to  take your strings and you can either  modify them or you can find out  different information about them and  these are going to be very useful i also  just want to point out that you can use  these little methods on things other  than just variables so for example if i  came down here i could print out like a  string and i could say dot and now i  could say like up case and it’s still  going to work exactly like it worked  when we used it on that variable so now  when i run this it’ll do exactly the  same thing so you don’t have to have  these inside of variables in order to  use these different methods so those are  just a couple of the different methods  that you can use with strings in ruby  what i would recommend is going on  google and just typing in like ruby  string methods and you know there should  be like huge lists of all the different  methods that you can use with these  strings but i would say for the most  part those are some of the most common  methods that you’re going to be seeing  and really i just wanted to give you  guys an introduction into how we can  work with strings inside of our programs  i wanted to talk about how we can use  different methods to do different things  hopefully you’ve learned something and  hopefully now you can go off and start  playing around with strings inside of  your programs  in this tutorial i want to talk to you  about working with numbers in ruby so  we’re going to take a deep dive we’re  going to look at all the different  things you can do with numbers talk  about the different types of numbers in  ruby we’re going to look at some awesome  ruby methods that we can use on our  numbers to do a bunch of different stuff  so this is going to be a pretty cool  tutorial but down here i’m just going to  show you guys the basics of working with  numbers um i could just say like puts  and this will just print something onto  the screen just so we can kind of see  what’s going on when i want to use a  number in ruby it’s really easy you just  type out the number so i could type out  like five for example and now this is  gonna get printed out onto the screen as  five in addition to just using whole  numbers like this we could also use  decimal numbers so i could say like 5.86  it’s gonna do exactly the same thing so  now over here it’ll be able to print  that out we can also use negative  numbers so i can put a negative in front  of here and again ruby’s gonna have no  problem dealing with negative numbers in  addition to just using numbers though we  can also use basic arithmetic so for  example i could say like five plus nine  and this will actually be able to go  through and print out the answer so not  only is ruby going to print out five  plus nine it’s actually going to do that  calculation for us and print the answer  out onto the screen which is pretty cool  so we can use addition we could use  subtraction we could use multiplication  which is just this asterisk we could use  division so those are like the four  basic types of arithmetic but we can  also use exponential so for example  let’s say i wanted to take two raised to  like the third power i can just do  something like this i can say two  multiplication multiplication so two  asterixes and then three and this is  basically going to be the same thing as  saying two raised to the third power so  now we should get two cubed which is  eight so it’s going to be a really handy  operation here we can also use something  else which is called the modulus  operator so i could say for example 10  a percent sign and then 3  and what this is going to do is it’s  going to take 10 it’s going to divide it  by 3 and it’s going to spit out the  remainder so 10 divided by 3 is 3 right  369 with a remainder of one so when i  run this you’ll see it prints out a one  so it’s basically printing out the  remainder that we get from dividing  these two numbers and that can be pretty  useful in addition to just having  numbers over here like this i could also  store numbers inside of a variable so i  could come over here and say like  num this is the name of the variable we  could set it equal to like 20. and then  down here if i wanted to access that  number i can just print out the variable  so now we’re printing out the actual  variable and i want to point out  one cool thing with numbers is you can  actually print out numbers and strings  inside of the same print statement so  for example i could say like  my fave num  and over here i can put a plus sign and  what this is basically going to allow me  to do is print out this string alongside  this number but you’ll see over here  when i actually print this out we’re  going to get a couple errors and the  first thing we have to do is we also  want to put this inside of parentheses  so i’m going to surround  this whole thing with parentheses that’s  because we’re referring to this variable  but also if i want to print out a number  alongside of a string i actually have to  convert this number into a string and i  can do that by saying num dot to  underscore s  and basically what this is going to do  is it’s going to take this number it’s  going to convert it into a string and  then we’ll be able to print both of  these side by side so you can see now it  just says my fave num 20. that’s a  really useful way to do something like  that so in addition to just working with  normal numbers we can actually use  special things called methods the method  is essentially just a block of code that  we can call which will either modify our  number or it’ll give us information  about our number sometimes it’ll also do  like mathematical calculations on the  number so down here i could say like  puts num when i want to access one of  these number methods i can say dot and  then i can type out the name of the  method that i want to use so for example  i can say num.abs  and then open and close parentheses and  basically what this is going to do is  it’s going to tell me the absolute value  of num so if i put like a negative 20 up  here  now this is just going to return 20  because it’s giving me the absolute  value there’s also some methods that we  can use with decimal numbers so for  example this was like 20.487  i can use  num.round and this will actually round  the number for me so over here you’ll  see we just get 20 so it’s basically  rounded the number but if i was to make  this like a six now we should get 21 so  it’s rounding it up or down you can also  use a ceiling and a floor function so a  ceiling will always take the higher  number and the floor will always take  the lower number so for example if i  said twenty point one eight seven let’s  just say twenty point one and i said num  dot c e i l  this is actually just gonna return 21 so  it’s going to give us the highest number  or it’s going to give us the next  highest number from 20. if i was to say  like 20.9 and i said num.floor  this is just going to give me 20. so  it’s basically just going to cut off  that decimal point give me the lower  number in addition to just using these  normal methods or you also hear these  refer to sometimes as functions so in  addition to using these methods or  functions we can also use other special  methods which are inside of something  called the math class and don’t worry  too much about what a class is but  essentially there’s this entity inside  of our ruby programs called math and we  can use it to perform specific math  operations on numbers so if i came over  here i could type out math like that  with the capital m i can type a dot and  now inside of this math class and again  don’t worry too much about what a class  is but inside of this math entity in  ruby there’s a bunch of these methods  that we can use so for example i could  say math.sqrt  and open and close parentheses now  inside of this open and close  parentheses i can give this a number so  for example i could put like 36 inside  of here and what this is going to do is  it’s going to give us the square root of  this number 36 so you can see over here  we get 6. you could also use something  like the logarithmic function so i could  say math.log put like a 1 in here and  now we’re going to get 0.0 back so  there’s a lot of these different like  math operations you can do a lot of  things with like sine cosine tangent  like logarithmic stuff so this can be  really useful if you’re just trying to  work with math inside of ruby so the  last thing i want to talk to you guys  about in this tutorial is working with  uh floating point numbers and working  with integer numbers so there’s two  basic types of numbers in ruby we have  integer numbers which are like whole  numbers so an integer would be like 20  or 21 or 22  and we also have decimal numbers which  would be like 22.1 22.2 etc integers and  floats can be used inside of ruby  basically interchangeably although there  are two like separate types so ruby does  distinguish between a whole number and a  floating point number but i want to show  you guys how we can use these together  so for example like if i came down here  and i just added two integers like i  added one plus seven you’ll notice that  we’re gonna get an integer back so it’s  going to be 8. but if i added like 1.0  and 7 now you’ll see we’re getting a  floating point number back so we’re  getting a decimal back so whenever you  add two integers you always get an  integer back or multiply two integers  divide subtract two integers you’re  always going to get an integer back so  for example if i said like 10 divided by  seven  this shouldn’t be an integer number this  should be like some long decimal number  but you’ll notice that i’m just getting  a one back so i’m only getting an  integer back but if i said like 10  divided by 7.0  now i’m going to get the full number  back so whenever you’re using an integer  and a floating point number together to  do some operation you’re always going to  get a floating point back if you’re  using two integers you’ll get an integer  back and if you’re using two floats  obviously you’re going to get a floating  point number back so that’s kind of the  difference between those two and ruby  doesn’t really give you too much of a  hassle when you’re using numbers you can  basically use integers and floats  interchangeably but just know that ruby  does distinguish between the two of them  so if you’re doing different types of  math with different types of numbers  like integers or floating point numbers  there’s going to be a difference in the  type of answer that you’re going to get  so that’s really the basics of working  with numbers i mean i could spend all  day talking about all the different  methods and you know little caveats here  and there that you can use with numbers  and ruby but i think that’s kind of a  good coverage of the basics so now your  job is just to go off and play around  with all this different stuff and really  just get comfortable working with  numbers in your ruby program  in this tutorial i’m going to talk to  you guys about getting input from users  in ruby this is going to be awesome  basically going to allow a user to input  information into our programs we’re  going to store whatever they input into  a variable and we’re going to print out  that variable along with a message that  basically just says hi to whoever  entered the information  so in order to get information from the  user we’re actually going to have to do  one thing and  if you’ve been following along with this  course we’ve been using this atom runner  program in order to run our ruby files  and this is a really awesome convenient  way in order to just run a ruby file but  here’s the problem if we want to get  input from a user inside of our ruby  programs we actually can’t use this  little atom runner plugin to do it we’re  gonna have to use something called the  terminal or the command prompt now if  you’re on windows this is a program  called the command prompt if you’re on  mac it’s called the terminal basically  it’s an environment where we can  interact with our computer using text  and in order to interact with the  computer and input information into our  ruby programs we’re going to have to use  the terminal so the first thing i want  to do is just show you guys how to set  that up and then we’ll look at getting  input from the user so  this is going to be instructions for  doing this inside of atom over here i’m  just going to go over to the preferences  inside adam or the settings  and down here i’m just going to click  this install tab and i want to search  for a package we’re looking for a  package which is called  platformio hyphen ide  terminal so search for this  and you’ll see it shows up over here and  i actually already have it installed  but you want to install this platformio  ide terminal basically what this is  going to allow us to do is use a  terminal or a command prompt if you’re  on windows straight from inside adam so  install that and now we’re just going to  go back over here  you might need to restart your atom  program in order to use it but  eventually what you should get is a  little plus sign you’ll see there’s this  plus sign down here at the bottom left  and when i hover over it it says new  terminal so once you have this installed  you want to go ahead and click that  and a little terminal window should pop  up down here you’ll notice mine is just  black with white text  so this is where we can run our ruby  program in order to get input from a  user and this is also another way that  you can run your ruby programs so down  here  as long as you have your ruby file open  inside of adam so as long as this file  is open when you open up platformio ide  terminal into a new terminal it should  automatically open up to the location  where your ruby file is now you know i’m  not going to get too into like using the  terminal in this tutorial but  essentially you can navigate through the  different folders and the different  files inside of your computer using the  terminal or the command prompt  so as long as you have your ruby file  open like i have this draft.ruby file  open this should automatically open to  the correct directory so you won’t have  to worry about that so once we’re here i  just want to type in ruby  and then i want to type in the name of  the file that i want to run so in my  case it’s just draft.rb  and what this is going to do is it’s  going to run the file for us so now i  click enter it’s going to run the file  and you know it’ll basically stop we  don’t have any code up here so if i was  to print out like  hello now when i run this again i can  just type it in and click enter and  you’ll see that we get the program  running so that’s basically how we can  run a ruby program from inside of our  command line or inside of our terminal  so we’re gonna have to do this in order  to get input from the user  just so you guys know all right so let’s  talk about how we can get input from the  user basically i’m going to allow the  user to input a piece of information  we’re going to store that piece of  information inside of a variable and  we’re going to print it out onto the  screen so first thing i want to do is  actually just type out a prompt so i  want to tell the user what i want them  to enter so i can just say puts and i’m  just going to say enter your name  and now once we’ve prompted them to  enter some information i can use a  special command in ruby called gets  and basically what this is going to do  is it’s going to allow the user to enter  a piece of information  into our program so it’s basically going  to stop the execution of the program and  wait for the user to enter something and  what i want to do is i want to store  whatever the user enters inside of a  variable so i’m actually going to create  a variable called name and i’m going to  set it equal to gets and basically what  this is going to do is it’s going to  tell ruby to take whatever the user  inputs into the program and store it  inside of this name variable so the last  thing i want to do is come down here i’m  just going to say puts and i’m going to  print out  some text so i’m going to say hello  plus  name so basically i’m printing out hello  to whoever entered in information into  the program so i’m going to save this  now i’m going to come down here and i’m  just going to run this program so you’ll  notice um if you click the up arrow on  your arrow keys on your keyboard it’ll  actually just insert the last line that  you entered so i just click the up arrow  here and now when i click enter you’ll  see that it says enter your name so i’m  going to enter my name it’s going to be  mike and now when i click enter and  actually we’re getting an error here  this should actually be puts down here  not put that’s my bad  so let’s do this again  so i’m going to enter my name mike and  now when i click enter it’s going to say  hello mike so it’s basically just saying  hi to me so that’s the basics of getting  input from the user you can use this  gets in order to get specific  information now i do want to talk to you  guys about one more thing which is  basically something that happens in ruby  when we enter information let me sort of  illustrate this so down here i’m saying  hello name and after this why don’t we  print something else out so i’ll say  like  you are cool right so i’m basically  printing out hello name you are cool  now i’m going to go ahead and run this  program and you guys will see what  happens so i’m running the program i’m  entering in my name  mike  now when i click enter you’ll notice  that it prints out hello mike but then  it prints out a new line and it says  that other text basically what’s  happening here is when i click enter  ruby is not only taking that as the text  that we entered but also as a new line  so whenever you click enter it basically  like is going to insert a new line so  ruby is essentially printing out mic and  then the new line character and then all  the text over here  in order to mitigate that all i have to  do is come over here and say name is  equal to gets dot  chomp and open and close parentheses and  this is going to get rid of that new  line character that happens when we  click enter so now when i run my program  it’s going to be able to work correctly  so i’ll say enter your name mike and now  it just says hello mike you are cool so  now our program is working perfectly so  if you want to keep that new line when  the user clicks enter you can go ahead  and just not put dot chomp here but in a  lot of cases you’re going to want to go  ahead and just put that in there so  that’s the basics of getting input from  a user if i wanted i could get multiple  pieces of information so for example i  could say puts enter your name and i’m  actually just going to copy this  and now we’ll say like enter your age  and we’ll do the same thing i’ll store  it in a variable called age so now we  could actually come down here and we  could print out like hello mike ur  and then we could just print out like  the age so now it’ll just be printing  out age  so  now we can get two pieces of input from  the user so let’s run our program and it  says enter your name  mic  and let’s say that i’m like 59. so now  it’s printing out hello mike you are 59  so we’re getting  input from the user we’re getting two  pieces of input from the user and we’re  printing them out onto the screen  so that’s the basics of getting input  and in the next couple lessons we’re  going to talk about other ways that we  can do this and basically ways we can  make this work a little bit better  in this tutorial i’m going to teach you  guys how to build a very basic  calculator in ruby we’re basically going  to build a little program that will  allow the user to enter in two numbers  and then we’ll print out the sum of  those two numbers so we’ll get the two  numbers from the user we’ll add them  together and we’ll tell them what the  answer is it’s gonna be pretty cool and  it’ll kind of give you guys uh some more  information about getting input from  users specifically how we can get  numbers from users so keep in mind in  this lesson we’re going to be using the  terminal or the command prompt  in order to get information from the  user so you want to make sure that  either if you’re using atom you can use  this platformio ide terminal plugin that  i talked to you guys about in the last  lesson or you can just use your normal  like terminal or your normal command  prompt so i’m going to be using this  little atom plugin that i have so let’s  talk about how we can get input from the  user specifically we need to get two  numbers so down here in our program we  can just write out the code to do that  so i’m gonna first write a prompt i’m  just gonna say puts  and we’re just gonna type out a message  for the user so we’ll say enter a  number  and then we’re basically going to get  that number that they entered and store  it inside of a variable so i’m going to  create a variable called num1 and i’m  going to set it equal to  gets dot chomp  and  gets is basically going to  get whatever information the user enters  in and chomp is going to get rid of the  new line that they accidentally enter  when they click the enter button so  whenever you click enter and ruby and  you’re inputting information into the  program it’s going to add a new line  onto the end of whatever you input so  this is just going to get rid of that  for us the next thing i want to say is  another prompt so i’m going to say puts  and i’m going to tell them to enter  another number  and once again we’re going to do the  same thing so i’m just going to say num2  is equal to gets.shop so essentially  we’re asking them to enter in two  numbers now all we want to do is just  print out the result of those two  numbers so i’m going to put and i’m  going to put this inside of parentheses  and i’m going to say num1 plus num2  right essentially what we’re doing is  we’re getting two numbers and then we’re  printing out the sum of those two  numbers onto the screen so let’s go  ahead and do that i’m going to come down  here into my terminal window  and i’m just going to run this program  so i’m just going to type out  ruby  draft.rb and this is going to run the  program for me so it says down here  enter a number so why don’t we enter  five  enter another number let’s enter two and  now when i click enter this should add  the two numbers together and print out  the answer so click enter  and you’ll notice down here we’re  actually getting a little bit weird of  an answer we’re getting the answer 52  and last time i checked 5 plus 2 wasn’t  52. essentially what we’re doing is  we’re  concatenating these two strings together  so here’s the problem when you enter in  information into ruby ruby automatically  just converts whatever you enter in into  a string so by you can enter in as many  numbers as you want you can enter in  decimal numbers normal numbers it  doesn’t matter ruby’s just going to  convert it into a string  and when we add two strings together  like this inside of a put statement it  just does what’s called concatenating  the strings so it takes the first string  and adds the other string onto the end  of it right so that’s why we get 5 2.  in order to actually add these two  numbers together we’re going to have to  convert the strings that the user enters  into numbers so over here we’re getting  a number num1 here we’re getting a  number num2 but remember when we  actually store those variables they’re  getting stored as strings so all we have  to do is take num1 convert it into a  number take num2 convert it into a  number and then we’ll be able to run  this program successfully so all i have  to do to do that is just say num1.2  an underscore and then an i so it’s  num1.2i and basically what this means is  we’re converting num1 into an integer  i’m gonna do the same thing over here  dot 2  i so now we’re basically taking these  two strings num1 and num2 and converting  them into integers so ruby’s going to  take whatever the numbers that were  inside of those strings and convert them  into integers so let’s go ahead and run  our program  so i’m going to come down here i’m just  going to type in clear  and i’m going to type in ruby  draft.rb  so it says enter a number we’ll enter in  five enter another number let’s enter in  two  now when i click enter you’ll notice  that we’re getting the answer which is  seven so that’s pretty awesome right we  were able to build our calculator and it  was able to add the numbers together  here’s the problem though let’s say i  come down here and i run this program  again and i enter in a 5  but now i enter in a 2.5 so for example  instead of just entering in 2 i’m  entering a decimal number 2.5 when i run  this program now you’ll notice that  we’re still getting seven so our program  wasn’t able to add this z this .5 on to  the answer  that’s because over here  we’re converting these to integers so  we’re saying num1 is going to get  converted into an integer num2 is going  to get converted into an integer and so  really all this is doing is it’s adding  in the integer 5 and it’s adding in the  integer value of 2.5 which is just going  to be 2 together so if i want to be able  to add in decimal numbers in my program  instead of saying 2i i’m going to say 2f  and 2f is basically going to convert  whatever is inside of those strings into  floating point numbers so into decimal  numbers so now when i run this program  down here  we’ll be able to do that  edition so if i say ruby and i’m just  going to enter in a 5 and i will enter  in that 2.5 and now this should give us  the number that we wanted so we’re going  to get 7.5  so  2f or two i or two really useful little  functions that we can use on numbers and  in some cases you’re only going to want  the user to be able to enter in integers  so you can just say 2i in other cases  though you want them to use floating  points so we can use 2f so that’s the  basics of building our little calculator  now i also want to point out one way  that we can make this program a little  bit easier so instead of saying  num1.2f down here i could actually come  up here  and i could say gets dot chomp  dot 2 f  and i could do the same for this one  over here dot 2  f  and now this is going to convert them up  here so we don’t have to convert them  down here so this is going to work  exactly the same as it worked before so  i could say like 5.6  and 7.2 and it’ll be able to add them so  that’s just another way that we can do  that but that’s sort of how we can build  a basic calculator  inside of our ruby programs  in this tutorial i want to talk to you  guys about building a mad libs game in  ruby we’re going to build a little mad  libs game and i’ll kind of show you some  more about how we can get input from a  user so if you’re not familiar with mad  libs a mad libs is basically just a game  where you would enter in like a bunch of  random like words maybe like nouns verbs  adjectives adverbs and then you’d take  all of those words that you’d enter in  and like sort of sprinkle them in inside  of a story and generally like since  you’re entering in random words the  story is going to be like kind of funny  so over here i have an example of a mad  lib you can see down here it’s like  telling the user to enter in like a noun  a plural noun an adjective and we’re  actually can build a mad libs game  inside of our ruby program so down here  you’ll notice i have a little basic  program set up it’s basically printing  out this poem roses are red violets are  blue i love you  and so this is you know kind of a  classic poem but i think we can mix it  up a little bit and create a mad libs  for this poem so how about we say  instead of roses or red we’ll let the  user enter in a color  instead of saying violets are blue we’ll  let them enter in a  plural noun  and instead of saying i love you why  don’t we let them enter in like someone  else so maybe like a celebrity instead  of having just roses or red violets or  blue i love you we’ll have roses r and  whatever the color the user enters in  whatever the plural noun they enter in  are blue and then i love a certain  celebrity so this should be kind of  funny  so let’s set up our program the first  thing i want to do is get input from the  user so i want to be able to get three  pieces of information from the user i  want to get the color i want to get the  plural noun and then i want to get the  celebrity after we get that input then  we’ll intersperse those variables inside  of this little output so let’s get the  input from the user i’m just going to  come over here and we’ll just print out  a prompt it’ll just say  enter a color  so the first thing they’ll do is enter  the color and now what we’re going to do  is store that in a variable called color  so i’ll say color is equal to  gets dot chomp and remember chomp is  just getting rid of the new line that  gets entered when you click the enter  button  all right so we’ll enter in a guess and  we’re also going to do two more of these  so i’m just going to paste this two more  times and then we’re going to enter in a  plural noun  and we’ll make a variable called plural  noun  and finally they’re going to enter in a  celebrity  so we’ll make a variable called  celebrity so now we have three variables  that are storing the three pieces of  information that the user input  last step is to come down here  and instead of just printing out color  inside of curly brackets i’m actually  just going to add in that color variable  so we’ll print out color  same thing for the plural noun and same  thing for the celebrity now our program  is essentially set up we’re getting the  input and then we’re just printing out  the actual bad libs so remember whenever  we  get input from the user i have to use my  terminal down here so instead of just  using this little like atom runner  plug-in that we’ve been using throughout  the course i’m going to use my terminal  and down here i’m just going to go to  the directory where my ruby file is  stored and i can just run it so i’m just  going to say ruby draft.rb that’s the  name of the file and i’m going to run  the program so it’s gonna ask me to  enter a color so why don’t we enter in  like magenta  it says enter a plural noun i’m gonna  enter in  microwaves  and enter a celebrity why don’t we do  like tom hanks  so now what should happen when i click  enter is all of that information should  get put inside of our mad libs and we  should get the finished story so i’m  going to click enter  and you’ll see down here we get our  finished story so it just says roses are  magenta microwaves are blue i love tom  hanks so basically the user could run  this program they could enter in all the  words for the mad lib and then we’ll  basically just be printing out the  madlib so it’s actually a pretty simple  program you can see how easy it is to  build a game like a mad lib inside of  ruby it’s super easy and also if you  wanted you could make this a lot more  complex so you know you could add it  have them add in like adverbs verbs  adjectives like all sorts of stuff  inside of here make obviously the story  a little bit longer and you could  essentially replicate any mad lib inside  of your ruby program which is awesome  in this tutorial i want to talk to you  guys about using arrays in ruby in ruby  we’re going to be dealing with lots of  information and a lot of times when  you’re writing programs there’s going to  be a bunch of different values that you  want to store and keep track of and one  way that we can keep track of the data  inside of our programs is using a simple  variable here’s the problem with the  variable though it can only really store  one single value and so i could create a  variable i could store a value inside of  it but what happens when i have lots of  data it’s going to be kind of a pain in  the butt to have to create like hundreds  of different variables if i want to keep  track of and maintain hundreds of  different pieces of information and this  is where arrays come in an array is  essentially a structure or a container a  lot like a normal variable the only  difference being that an array can hold  multiple values so unlike a normal  variable an array can hold like 10 or 20  or 100 or a million different items  inside of it and a lot of times when  we’re programming in ruby and we’re  dealing with large amounts of  information we’re going to want to be  able to use a raise so let’s jump in i’m  going to show you guys how to use arrays  we’ll talk about you know what they are  how to create them how to put stuff  inside of them all that fun stuff so  down here if i want to create an array  i created a lot like i would a normal  variable the first thing we have to do  is tell ruby what we want the array to  be called  so in my case why don’t we make an array  that’s going to store a bunch of names  of my friends so i can make an array  called friends  and i’m going to set this equal to  array with a capital a and then we’re  going to make an open enclosed square  bracket  inside of this open and close square  bracket i can start putting in some of  the pieces of data that i want to store  in this array in my case i’m just going  to sort a bunch of strings with names of  some of my friends so i can store one  like kevin  karen  and  oscar so here we have an array that has  three elements inside of it and each one  of these pieces of data are referred to  as array elements basically what i did  is i created a  container that can hold multiple pieces  of information so what i can do now is i  can come down here and i can just say  like puts  friends  and this is actually going to print out  all of the information inside of this  array all the data values on  my output over here so you can see we’re  printing out kevin karen and oscar so  that’s the basics of using an array and  if i didn’t want to just store strings i  could store any type of data inside here  so i could sort of strings i could even  store like a number or i could store a  boolean value like false you can put all  different types of information all  different data types inside of these  arrays and if you want you can even put  different data types together in the  same array but for our purposes let’s  just stick with strings so  let’s say that i wanted to access one  specific element inside of this array  well if i want to access just one  element i can make an open and closed  square brackets after i’m referring to  the variable name  and i can put an index inside of here so  let’s say that i wanted to grab kevin  let’s say i wanted to grab this first  string inside of my array i can put a 0  inside of here and 0 refers to the index  of this kevin value so now when i print  this out you’ll see we’re just printing  out kevin  and this brings me to a point about  arrays we start indexing arrays at zero  so i would say that this kevin attribute  or this kevin element inside of the  array is that index position zero  karen is it in exposition one and oscar  is at index position two so the first  element is always in index position zero  and then you just basically count up  from there so if i wanted to access  oscar over here i could put a 2 inside  of here and now this is going to print  out oscar  another thing i can do is i can access  array elements from the back of the  array so for example another way i could  access oscar would be by putting a  negative one in here  basically what this is gonna do is it’s  gonna grab the item in the array but  it’ll start counting from the back so  it’ll basically like this would be  negative one oscar this would be  negative two and this would be negative  3. so if i said negative 2 in here now  we’re going to be grabbing karen and  printing that out so that’s two  different ways that we can access  elements inside the array i could also  grab a range of elements so for example  let’s say i only wanted to grab the  first two elements inside this array and  i didn’t want to grab the third one i  could say  zero and then i could say two and  basically what this is gonna do is it’s  gonna grab index position zero all the  way up to but not including index  position two so it’s gonna grab zero and  one and it’s gonna stop at two so we’re  not gonna grab two so now we’re just  gonna get kevin and karen  and  this can be really useful just  specifying like a range inside of this  square brackets there’s also a bunch of  other stuff that we can do with these  arrays so for example let’s say i wanted  to modify one of the values inside the  array well i can come down here and i  can just say friends let’s say i wanted  to change this first element at index  position zero i could say friends  zero and i can give it a new value so i  could say like dwight and now instead of  having the value of kevin this is going  to have the value of dwight so when we  print it out we get that new value and  you can access and modify any individual  element inside the array just by  referring to its index  there’s also going to be situations  where you’re not going to know exactly  what elements you want to put inside of  the array right up front so for example  over here i knew exactly what friends i  wanted to put in this array but in some  cases you’re not going to know that  right off the bat so we can just say  array.new  and now we’re basically telling ruby  that we want friends to be an array but  that we don’t want to put any values  into it just yet  then down in my program i can just start  putting stuff in here so for example  right now if i printed out friend zero  you’ll notice that nothing prints out  it’s just a nil value right if i came  over down here though i could say  friends zero and i can just give this  whatever value i want so i can give this  a value of like michael and now  friend zero is going to be michael and  you can do that as much as you want so i  could even come down here and say like  friends  five and i could give this a value so  now this is gonna be equal to  holly  and what you’ll see over here when i  print out just the entire friends array  it’s going to fill in all of the  elements that are between 0 and 5 just  with blank elements so those are just  going to be nil so that’s kind of how  you can create an array and not give it  some initial information yet and then  add in information later there’s also a  bunch of different methods that we can  use with these arrays so let’s go back  to that array that we were using before  with all the friends so if i wanted to  access specific information about this  array i can use little methods so i  could say like friends dot and one  really useful one is  length so this will tell me how many  elements are inside of this array so you  see over here we have three i can also  check to see if certain elements are  inside the array so i could say friends  dot include and then i’m going to put a  question mark and over here i can type  in an element that i would want to check  to see if it’s in the array so i could  say like karen  and now this should give us a true value  because karen is inside of the array if  i put like karen’s though so i changed  it  now this is going to give a false value  because that’s not in the array you can  also modify the order of the array so i  could say like friends dot reverse and  this will reverse all the elements so  now it’s going to be oscar karen and  then kevin as you can see over here you  can also sort different elements in the  array so for example if i wanted i could  sort all the elements so if i were  working with strings we could sort them  alphabetically so if i put something  over here  like andy  and now andy’s going to end up being in  the front because it’s going to sort the  array alphabetically so i’ll say  friends.sort  and now when we print this out you’ll  see it’s andy karen kevin oscar so it  moved andy to the front because a comes  before k and o it’s important to note  though that if you’re going to have an  array that has multiple data types so  for example if i put a a number like an  integer in here this is going to throw  an error now because you can’t sort it  there’s no there’s no way for us to  compare like  numbers and strings inside of ruby so  that’s the basics of working with arrays  and arrays are super useful there’s  going to be tons of situations where  we’re going to want to store large  amounts of information in a single  container and arrays are great at doing  that  in this tutorial i want to talk to you  guys about using hashes in ruby now a  hash is basically a type of data  structure where we can store a bunch of  different pieces of information if  you’re familiar with arrays in ruby  they’re very similar to arrays so a hash  is basically doing the same thing  it’s allowing us to store multiple  pieces of information the difference is  that with hashes we can actually store  something called a key value pair  and a key value pair is basically where  we can store a value and we can give it  a key which is kind of like a name now a  lot of times you’ll hear hashes also  called dictionaries  that’s because they act a lot like  dictionaries  picture a dictionary like in a  dictionary you have two parts to every  word right you have the actual word  itself  but then in addition to the word you  actually have the definition right so  you could say that the word is the key  and the definition is the value so  that’s basically what a hash is and  you’ll kind of see  what this is as we kind of go through  this tutorial in this tutorial i want to  build a little hash which will store uh  state codes so for example i live in the  united states and we have a bunch of  different states and each state has a  specific code so for example  pennsylvania pa right new york is ny  california is ca michigan is mi right so  you can map a state like  new york  to a specific  abbreviation like ny we can map  california to a specific abbreviation ca  and that’s basically what i want to  represent inside of my hash so we’re  going to build a hash which can store  all of those different abbreviations and  you’ll see why these data structures are  useful and how they’re different from  arrays so down here i want to create my  hash and in order to do that you  basically just have to give the hash a  name so i’m just going to call this  states  and i’m going to set it equal to an open  enclosed curly bracket just like that  and a lot of times when people are  making hashes they’ll end up putting a  new line here  so inside of here we can specify a bunch  of different key value pairs okay so  what i want i want the keys to be the  actual state names and i want the values  to be the abbreviations for those states  so basically all we have to do is just  type out a key and then we can type out  a value so the first thing i’m going to  do is type a key  so why don’t we map like  pennsylvania and so pennsylvania is  going to be the key and now i can map  this to a value so i can just say equals  and then a greater than sign and over  here i can type in a value so i’m just  going to type pa okay so essentially  what i’m doing is i’m defining the key  and i’m defining the value now i’m going  to type a comma and i can go and define  another key value pair so why don’t we  do new york say new york  and we’re gonna map this to  ny  and why don’t we do another one oregon  is another state and we’ll map this to  or so in here i’m basically have three  different states and i’m not going to do  all 50 states but i have a key and then  i have a value now here’s one thing you  need to know about these hashes is you  can only have unique keys so for example  i couldn’t create another key down here  called pennsylvania that’s going to be a  big no-no when we’re creating hashes you  always want to have unique keys  all right so now that we have these  input into here we can actually start  using this hash so down here if i wanted  i could just print this out so i could  come down here and say puts and it’s  just called states so we can put this  and when i print this out you’ll notice  that we’re printing out this little  structure it’s just like pennsylvania  that gets mapped to pa new york gets  mapped to ny et cetera  but one of the most powerful things we  can do with these hashes is we can  actually give it a key  and it’ll tell us the corresponding  value so i could come in here and inside  of these square brackets i can just  input a key so i can input like  oregon and now this is going to print  out the value for oregon so it’s going  to print out or  i could put inside here  new york and now this is gonna print out  ny because it maps to new york so that’s  like a super useful way that we can  store our data we could give this  structure a key and it would spit out  the corresponding value there’s also a  couple other ways that we can create  these keys so for example instead of  putting pennsylvania inside of quotation  marks i could also just put a colon here  and now this is going to be the same  thing so down here i could specify like  pennsylvania and actually this should be  capital  and now it’ll still give me that same  value so it’ll still give me pa up here  you can also use in addition to like  strings we could also use numbers so  down here i could say one  and if i put one down here now we’re  basically going to get  the same thing so this should still  print out pa and you can see that it  does over there so these hashes are  extremely useful and there’s a lot of  situations where you’re going to want to  map keys to values so for example in  this situation we’re mapping a key which  is the state name to a value which is  the state abbreviation but you could do  the same thing for like days of the week  you could do it for month abbreviations  you could do it for i mean really  there’s tons of situations where this  type of structure is going to come in  handy and now i just want to point out  how these are different from arrays so  normally when i create an array i’m just  creating like a list of individual  values right  but when i create a hash i’m creating a  list of key value pairs  and i can give this hash a specific key  and it’ll spit out the corresponding  values so  this is a very useful data structure  this is definitely something you’re  going to want to play around with and  get used to using  in this tutorial i want to talk to you  guys about methods in ruby now a method  is basically just a block of code that  we can write which will perform a  specific task for us a lot of times in  ruby you’re going to have different  groups of code different groupings of  code they’re going to perform specific  tasks or they’re going to do certain  things and what we can do in ruby is we  can take all of that code that’s you  know designed to perform one task we can  put it inside something called a method  and what’s cool about methods is you can  actually call them from other places  inside your program and we can give  methods information and then they can  give us information back so in this  tutorial i’m just going to talk to you  guys about the basics of using methods  we’re going to write a method and we’re  going to look at how they can be useful  so the first thing i’m going to do is  create a method and our method is going  to be very simple all it’s going to do  is say hi to the user so our method is  going to perform one task it has one  goal and it’s going to say hi to the  user so down here i’m going to create my  method the first thing we always want to  do when we create a method is type out  def  and this basically means that we’re  defining a method second thing we want  to do is give our method a name so  generally you want to give your methods  descriptive names so it’s very obvious  what task they’re performing in our case  our method is saying hi to someone so we  can just call it say hi just like that  and now what i want to do is hit enter  and i’m going to come down here and i’m  just going to type end  so essentially what i’m doing is i’m  defining a method block  and anything that’s in between def and  end is going to be inside of our method  a lot of times people will indent here  just to show that the code is like  inside the method so in here again we  have one goal we want to say hi  so i’m just going to type puts and i’ll  just say hello user cool  so now i’m going to run my program and  we’ll see if this method works so i’m  just going to run the program  but you’ll notice over here when i run  the program nothing’s showing up and  that’s because when we define a method  the code inside of the method is only  going to get executed  when we call it so in other words if we  want to execute the code inside this  method we have to call the method that  basically just means we have to say like  hey method go do your stuff go perform  your task so i can come down here and  the way i call this method is just by  typing out its name so i can type out  say hi  and now ruby is going to execute this  method up here so you can see over here  we have hello user so everything’s  working and i just want to show you guys  one thing so if i typed out like top up  here  and then typed out bottom over here i  want to show you guys the flow of these  methods inside of our program so now  when i run this program you’ll see we’re  printing out top hello user and then  bottom essentially what’s happening is  ruby is looking at this line of code  it’s executing it then it sees say hi  so it knows okay the user wants me to  execute the say hi method so ruby’s  going to jump up it’s going to execute  all of the code inside of this say hi  method and then once it’s done executing  all of that code it’s going to jump down  back down here and it’s going to print  out bottom and i just want to point out  you could put as many lines of code  inside of one of these methods as you  want obviously this is like a simple  method so we’re just keeping it simple  but that’s the basic execution flow of  functions or methods and actually this  brings me to a good point a lot of times  these are called methods but you’ll also  hear people calling these functions  essentially in the case of ruby these  words are basically interchangeable  basically means the same thing but for  the most part in ruby we’re referring to  them as methods all right so let me show  you guys some more cool stuff we can do  one thing we can do is we can actually  give these methods some information so i  can actually take this say hi method and  i could actually allow the user to tell  it who to say hi to the way that i’m  going to do that is after i type the  name of the method say hi i’m going to  make an open and closed parentheses and  over here i just want to specify what’s  called a parameter a parameter is  basically a value that whoever is  calling this method is going to give to  it so this method can actually accept  inputs it can accept parameters  as input so over here i can just type  out the name of the parameter i want to  accept so in our case we’ll just call  this name because it’s going to be the  name of who we want to say hi to  and then down here instead of saying  hello user i can say hello  name  and you’ll see in a second this is going  to  use whatever variable or whatever  piece of information gets passed  into this method so down here  if i want to give this a name i can make  an open and close parenthesis and in  here i could type a name so i could say  mic so now when we run our program it’s  going to say hello mike  essentially what’s happening is the say  hi method is specifying that you can  give it a name  and down here  i can when i call the method i can give  it that piece of information you can  also give these things multiple pieces  of information so i could also specify  age  and then down here we can incorporate  that into our print statement so i can  say hello  name  you are and now we’ll print out their  age so i can just say age  and essentially this will take in two  parameters and it’ll print them both out  over there so down here i can just  specify the first parameter mic and then  i can specify the age so we could say  like you know 73 or something and now  it’s going to say hello mike you are 73.  and actually you’ll see we’re getting an  error over here and this is actually a  good little catch so  age i’m actually passing in an integer  and whenever we want to print out an  integer inside of a print statement like  this with strings we always have to say  age dot 2 underscore s so that’s why  we’re getting that error and that’s  something that you always want to watch  out for in ruby so now when we’re on our  program it says hello mike you are 73.  so that’s pretty cool we can pass this  two pieces of information but let’s say  that i didn’t want to pass this in age  right maybe i didn’t know how old mike  was so i didn’t want to give it an age  well now when we run our program you’ll  see that we’re getting an error right so  because i didn’t include an age when i  called this it’s throwing an error  one way we can mitigate this in ruby is  we can actually give these variables  default values we can give these  parameters default values so i could  come over here and say name is equal to  no name  and we can just say age is equal to  negative one  and essentially what’s gonna happen now  is if i don’t include an age in here  it’s just gonna print out the default so  it says hello mike you are negative one  right if i wasn’t including the name in  here it’s just gonna use both of those  default values so you’ll see i’m not  passing any parameters and now it  basically just says hello no name you  are negative one so sometimes specifying  default values can be a good way to  control these methods and really it  depends so in some methods you’re going  to want whoever’s calling them to give  you certain pieces of information but if  those pieces of information are like  optional you can just give them default  values like that so that’s the basics of  working with methods and there’s  actually one more thing we can talk  about i’m going to talk about it in the  next video something called return types  basically we can give the method  information and the method can give us  information back but for now this is  just the basics and i hope you guys  learned something about method  in this tutorial i want to talk to you  guys about using returns in ruby methods  so in the last tutorial we talked a lot  about writing methods we looked at how  we could give methods some information  through parameters in this tutorial i  want to talk to you guys about how  methods can give us information back so  when i call a method inside of my  program that method will go off it’ll  execute all its code but then it can  actually give us a piece of information  or in some cases multiple pieces of  information back and that can be really  useful so for the purposes of this  tutorial i want to show you guys how we  can create a cube function so in math  when you cube a number you essentially  take it to the third power so if i was  going to cube 2 it’d be like 2 raised to  the third power so essentially it would  just be 2 times 2  times 2 right so why don’t we write a  little method that’s going to do that  for us so this method will cube a number  this is going to be pretty interesting  so down here i’m just going to say def  and we’ll say cube  and we want to pass in one number so i’m  just going to specify that they need to  pass into num  and then down here we’re going to say  end what’s cool about these methods is  they can actually give us information  back so if i was going to cube the  number it would basically just be  num times num  times num right  so that’s essentially like all we would  need to do to cube the number but what’s  cool is when i call this cube function  and i give it a number so like i give it  a 2  if i was to print this out so if i said  puts cube 2  this is actually gonna print out  num times num times num so it’s actually  going to print out this answer  over here so let’s take a look you’ll  see here we’re printing out eight if i  was to pass in like three over here now  we should get 27.  so basically what’s happening is we’re  calling this cube method and this cube  method is giving us a value back so when  i call this this is actually like ends  up representing a value it ends up  representing the value that was given  back to it and if you want to give a  value back all you have to do is  basically just specify it right here so  that’s a really cool way that we can use  these methods to get information back  but sometimes when you’re working in  these programs it might not be super  clear what value you want to return so  for example i have num times num times  num here but if i put a 4 down here  right below this or even if i put like a  string or whatever let’s put a 5.  you’ll notice that now instead of  returning num times num times num this  is actually going to return 5  and that’s because 5 is the last line  inside of this method  it’s basically like the last returnable  piece of information that’s inside of  this method so in situations like this  you can actually use what’s called the  return work keyword so i can say return  right before here and even though  there’s a value after this you’ll see  that this is going to return num times  num times num anyway so we’re returning  27. and actually here’s the interesting  part is any code that goes before this  return keyword or that goes after this  return keyword isn’t going to get  executed so if i put a puts down here  and i printed out hello  when i run my program you’ll notice that  it doesn’t print out hello like nothing  is getting printed out  basically what’s happening is when we  use this return keyword that’s going to  signal to ruby that we’re done with the  method so when ruby sees this return  keyword it’s basically going to jump and  break out of the method and move on to  the next line of code so essentially  when you’re using the return keyword  nothing after it is going to get  executed and that’s just a little tip  so in addition to returning just normal  numbers like we did over here we can  also return multiple pieces of  information and keep in mind you can  return any data type it doesn’t have to  be like a number it could be a string  could be boolean it could be anything if  i wanted though i could return multiple  numbers so for example i could return  num times num times num and then if i  put a comma here i could return another  value so i could return like 70.  and now when i run my program in  addition to returning 27  you’ll see that it’s also returning 70  so i’m getting two pieces of information  and this is basically just returning  like an array so i could access each  individual value that got returned by  its index so if i said cube 3  square brackets 1 that’s just going to  give me 70. now you want to be careful  when you’re returning multiple values  just because if you’re returning like  five or six different values it can get  a little bit confusing especially for  you know the code that’s actually  calling these functions but for the most  part that can actually be pretty useful  so that’s the basics of using that  return keyword and also just returning  values in general this can be a super  powerful way to to make your methods a  lot better  in this tutorial i want to talk to you  guys about using if statements in ruby  an if statement is a special structure  that we can use in ruby in order to help  our programs to make decisions they also  help our programs to respond to the  different information in the program so  if certain information has certain  values we can do certain things if other  information has different values we can  do different things basically if  statements make our programs a little  bit smarter so in this tutorial i’m just  going to give you guys a basic overview  of if statements what we can do with  them how we can use them and essentially  just get you guys up and running with if  statements so over here i just have this  little text file  and uh it actually has some common if  statements that human beings every day  will encounter so believe it or not if  statements also apply to like human  beings so let me show you guys a couple  of these the first one up here says i  wake up  if i’m hungry  i eat breakfast so let’s break this down  a little bit you’ll see right here we  have a condition  right if i’m hungry  this condition is either true or false  right on the condition that they’re  hungry  then they’re going to go ahead and eat  breakfast if they’re not hungry though  if this condition is false then we’re  just going to move on and they’re not  going to eat breakfast right that’s  something that pretty much every morning  everybody is going to have to like ask  themselves at some point here’s another  one it says i leave my house  if it’s cloudy i bring an umbrella  otherwise i bring sunglasses so up here  you’ll notice the same pattern we have  another condition  right the condition is if it’s cloudy if  that condition is true if it is indeed  cloudy then they’re going to bring the  umbrella but if the condition’s false in  other words if it’s not cloudy then  we’re going to come down here and we’ll  bring the sunglasses so this one’s a  little bit different from the one up  above because if this condition if it’s  not cloudy if this is false  then we’re going to do something else  all right so i have one more down here  at the bottom it says i’m at a  restaurant  if i want meat i order a steak otherwise  if i want pasta i order spaghetti and  meatballs otherwise i order a salad so  this one’s even more complex right we  have our condition  it says if i want meat if that’s true we  order the steak  otherwise  if that’s false in other words if you  don’t want meat then we check another  condition so we’re checking to see if we  want pasta if that’s true we get the  spaghetti and meatballs finally though  if this is false  we get the salad so these are three  basic if statements and these are three  if statements that we could actually use  and these are the types of things that  we can do inside of our program so we  can specify certain conditions if those  conditions are true we can do certain  things those conditions are not true we  can do other things so i’m going to show  you guys basically how this works let’s  go over here to our ruby file so i’m  just in my draft.rb file  and i’m going to create a if statement  so i’m going to show you guys how this  works the first thing we’re going to do  before we actually make the if statement  is just make a variable so i’m just  going to call it ismail  and this is going to be a boolean  variable it’s going to tell us whether  or not someone is male so let’s say this  is a piece of information that we were  storing inside of our program like  whether or not someone’s male so in my  case i am a male so i’m going to set it  equal to true  so down here we can actually make an if  statement so we can do something  depending on the value of this variable  is male so i can say if  and over here i want to specify a  condition  so remember when we were looking at  those text files it was like if it’s  cloudy or if i’m hungry or if i want me  right we were giving these those  statements different conditions in this  case we’re going to specify our own  condition so i’m going to say if  is male  and inside of this condition we need to  include either a true  or a false value so booleans are great  with this because a boolean can either  have a value of true or false so i can  say if is male  and then down here i just want to type  out end  now whatever goes in between if and end  is going to get executed when is male is  true so over here we could just put you  are male right and so now because ismail  is true when i run my program you’ll see  over here it says you are male so the  program is working if i was to change  ismail to false however so i’m just  going to put false now you’ll see that  this code isn’t getting executed so  we’re not printing anything out that’s  the basics of an if statement right i  can specify a condition  if the condition is true  then we’ll execute the code down here if  the condition’s false we just skip over  it so i also want to show you guys um  how we can use something called an else  so basically what happens if we want to  do something when the person’s not male  right so we have it covered if the  person’s male we’re going to tell them  they’re a male but what if they’re not a  male well i can actually come down here  and i can type out else so i’m just  going to say else  and then again below else i’m going to  put code so i could say puts  you are not male  so now we basically have an if statement  that will handle both of the  possible scenarios so if they’re male  it’ll tell them they’re male otherwise  it’ll tell them they’re not male so  you’ll see ismail is false and over here  we’ll we’re telling them that they’re  not male if i was to change this back to  true  now it’s going to say you are male so  it’s essentially able to react to the  value of that variable  let’s make this if statement a little  bit more complex so why don’t we add in  another boolean variable i’m going to  say is tall  and we’ll set is tall equal to actually  let’s set it to true initially we’ll  also set this to true  so istal will tell us whether or not a  specific person or specific entity you  know is tall  so what happens down here if we want to  do something when they’re male and  they’re also tall i can actually use  another keyword in ruby which is called  and so i could say if is male  and  is tall and basically what this is going  to do is it’s going to execute the code  inside of here  when they’re both male and they’re also  tall so here we could say you are a tall  male and so now this code is only going  to execute when is male is true and is  tall is true so they’re both true right  now let’s run our program  it says you are a tall male  but if i was to set one of these equal  to false so for example if i set in is  tall equal to false  now it’s not going to tell us that we’re  a tall male anymore so it’s just going  to say you are not male although i guess  we would have to change this down here  so we we can say you are  either not male or not tall or both  and so now it’s telling us you’re either  not male or not tall or both because we  know for a fact that they’re not male  and tall so that’s how we can get we can  check two conditions and in addition to  using an and i could also use another  keyword called or  basically this will execute if they’re  either male or  if they’re tall so only one of these  variables has to be true now right this  is going to execute if they’re male or  if they’re tall so you’ll see over here  is male is true and is tall is false but  this is still going to execute you are a  tall male and obviously we could change  you know whatever’s getting printed out  there but hopefully you get the point  when we say and both of the conditions  have to be true so if i was to say is  male and is tall these would both have  to be true in order for this code to get  executed if i say or  only one of them needs to be true now  both of them can still be true and will  still execute that code but only one of  them has to be true and if both of them  are false then we’ll execute the code  down here so i’m going to go back to  ismail and is tall and let’s say that i  want to check to see the other  conditions right so we’re already  checking one condition we’re checking to  see if they’re male and they’re tall but  what if we wanted to do something when  they’re male and they’re not tall  well i can use something called an else  if and an else if is basically another  keyword in ruby that will check another  condition so i could say like  e-l-s-i-f and this just stands for elsif  and i want to type in another condition  after this so why don’t we check to see  if they’re male so i’m going to say else  if is male  and i want to check to see if they’re  not tall and the way that i can check to  see if they’re not something is i can  use a exclamation point so if i said  and exclamation point is tall  this is basically saying not is tall  so when they’re not tall this is going  to be true and we’re doing that using  this exclamation point it’s called the  negation operator so down here we can  put some more codes so i could just say  puts  you are a short male  right  basically what’s happening is it’s going  to check this if condition so ruby’s  going to check this if statement up here  if they’re either not male or they’re  not tall  so in other words if this whole thing is  false it’s going to come down here and  it’s going to check this next condition  and if this is false it’s going to go  down to the else but if this is true  it’ll execute the code inside here  there’s actually one more scenario that  we could cover which is when they’re  tall but they’re not a male so i’m going  to use another elsif i’ll say else if  not is male so i’m using that  exclamation point and is tall  and if this is true  then we can just  print out  you are not male but are tall  and then also down here if the code  inside of this else block is getting  executed then we’re gonna know that  they’re not male and not tall so we can  say you are not male and not tall all  right so let’s go ahead and run our  program so i’m going to set is male  equal to true and is tall equal to true  right off the bat just to start so i’m  running the program and you’ll see it’s  able to handle that situation it says  you are a tall male if i was to set  ismail equal to false though our program  is going to be able to handle that  situation so it’s going to say you are  not male but are tall right our program  just by using these if statements was  able to give us information based off of  these values  if i was to make  is tall false and ismail true  it’s going to be able to handle that so  it’ll say you are a short male  if i make both of these false  it’s going to be able to handle that as  well so it’s going to say you are not  male and not tall so that’s the basics  of using if statements you can see down  here i’m checking a condition and in a  lot of cases you’re going to want to  check more than one condition so you can  use either and or or  i’m checking these conditions  if the condition is true  then we’ll execute the code down here  and by the way you can put as much code  down here as you’d want i mean i could  put hundreds of lines of code if i  wanted to  so i’m using the if statements and i’m  using these elsifs and i’m using this  else in order to catch  all of these different scenarios and  these elsif’s are really useful because  you can check  multiple conditions when other  conditions are false so that’s the  basics of working with if statements  there’s actually some more stuff we can  learn with these um instead of just  using booleans like true and false we  can also use things called comparisons  and i’m going to be talking about that  in the next tutorial but for now this is  just kind of been a brief introduction  into if statements in ruby  in this tutorial i want to talk to you  guys some more about if statements  specifically we’re going to be talking  about using comparisons with if  statements so in any if statement you’re  going to need a condition and a  condition is basically a true or a false  value one of the cool things about ruby  though is we can use a comparison in  order to get a true or false value so  inside of our condition and our if  statement we can actually compare  different values and depending on the  result of that comparison we’ll be able  to either execute the code inside of the  if statement or move on so i’m going to  show you guys how we can do this it’s  really awesome and it’s a super powerful  feature what i want to do in this  tutorial is actually create a method so  i want to create a method called max and  this method is going to take three  numbers as input so it’s going to take  three parameters and basically what it’s  gonna do is it’s gonna return whichever  number is the largest so whichever  number out of the three is the biggest  it’ll basically return that back to the  user and this is a great example of a  situation where you’d want to use  comparisons inside of your if statements  so let’s get started i’m just going to  come down here and we’ll start making  our function or our method so i’m just  going to say def and we’re going to call  it max and i’m going to make an open and  close parentheses inside of this  parentheses i can specify any parameters  that i want this method to take in so  i’m just going to say num1 num2 and num3  basically we’re taking in three numbers  now down here  i’m just going to say end and inside of  this method we have to devise an  algorithm to figure out which number is  the biggest right so inside of this  method we don’t know which of these is  the biggest right we have no idea we  have no idea what the user input into  here so we need to use an if statement  in order to figure that out so i can say  if  now inside of this condition i can  actually compare  two numbers so i could say if num1  is greater than or equal to  num2  and  num1 is greater than or equal to num3  so  remember inside of an if statement we  need to put a condition here  and in the last tutorial when we were  using conditions we were putting in true  or false boolean values what you’ll  notice is when i compare two values when  i compare num1 and num2 this is actually  going to get resolved into a true or a  false value so this comparison is either  true or it’s false num1 is either  greater than or equal to num2 or it’s  not this is actually going to end up  being a boolean value this is going to  end up being true or false same thing  over here num1 is either greater than or  equal to num3 or it’s not it’s a boolean  value there’s only two possible  situations here it’s either true or it’s  false we didn’t actually have to put  like true or false here we were able to  use a comparison  in order to get a true or false value so  if num1 is greater than num2 and it’s  greater than num3 then we can just  return  num1 because we know that’s the maximum  number  down here i’m just going to say  elsif and i want to check another  condition so i’m going to check to see  if num2 is greater than num1 or actually  greater than or equal to num1  and  num  2 is greater than or equal to num3  so  if num2 is bigger than num1 and num3 in  other words if the result of both of  these comparisons  is true  then we know that num2 is the biggest  number so we’re just going to return  that  and finally  we can use an else statement so i can  use that else keyword i could just say  else and down here we can just return  num3 because if num1 isn’t the biggest  and num2 isn’t the biggest then we can  be pretty sure that num3 is the biggest  so  this is basically how we can use  comparisons inside of our if statements  i also want you guys to notice that i  was able to use this return keyword  multiple times right so if this  condition is true we can just return the  number all right so let’s go ahead and  run this function so i’m gonna come down  here and i’m gonna call this function  i’m gonna say max and i’m gonna pass it  three numbers one two and three  so  if i print this out i print out the  value that gets returned actually i’m  gonna put it  i should get a three back so let’s go  ahead and run this and actually you’ll  see i’m getting an error over here i  forgot to put in the end keyword so down  here we just have to put in end so  you’ll see we have an end keyword here  and this ends off this if statement we  have another end keyword here and this  ends off this function declaration so  that’s something to keep in mind you  always want to make sure that you’re  remembering to include this end keyword  you can see how easy it is to forget i  just forgot it so  i’m gonna run this program and you’ll  see we’re getting three so i passed it  three numbers if i was to make this  middle number the largest so we’ll make  it 20 now it should return 20. if i was  to make the first number the largest  it’s going to return that so we now have  a valid working max function that will  always tell us what the maximum number  is  so i just want to point this out one  more time what was happening instead of  putting a true or a false value in there  we actually indirectly put a true or a  false value in there by making a  comparison and the result of that  comparison is either going to be true or  false there’s no other situation right  num1 is either big greater than or equal  to num2 or it’s not i’m also using  something called a comparison operator  so this right here is a comparison  operator it’s an operator that we can  use to compare different values this one  is greater than or equal to but there’s  also some other ones so for example  probably the most basic is a double  equals and this basically means equal to  i can put a exclamation point in equal  sign this means not equal to so this  would be like num1 is not equal to num2  we can do a greater than sign a greater  than or equal to sign a less than sign a  less than or equal to sign and all of  those are going to allow us to compare  different values it’s also important to  note that in addition to just using  numbers you could also use strings here  so you can compare  different strings  for example i could compare two strings  for a quality i could check to see if  one string was equal to another string  and that’s really a powerful way that we  can use these if statements is by  checking comparisons a lot of times in  your programs you’re going to want to  compare different values and depending  on the results of those comparisons do  different things  in this tutorial i want to talk to you  guys about creating a better calculator  in ruby if you’ve been following along  with this course then you’ll know that  in the beginning of the course we  created a very simple calculator  basically we allowed the user to input  in two numbers we took those numbers  added them together and then printed out  the answer onto the screen so it was a  really simple calculator but in this  tutorial we’re going to be building an  even better calculator this calculator  is not only going to be able to add two  numbers but it’ll be able to add  subtract multiply and divide numbers and  will allow the user to choose which one  they’re going to do this is going to be  pretty cool and we’re going to use a lot  of the stuff that we’ve learned up to  this point in this course in order to do  it so  the first order of business when we’re  creating our calculator is we need to  get some input from the user i need to  get certain information right we need to  get the first number we need to get the  second number and then we need to get  the operation that they want to perform  so they would need to type in like two  and then a plus sign and then a five you  know something like that basically so  let’s go ahead and do that down here in  my program the first thing i want to do  is just print out a prompt so i’m gonna  say puts  and we’re gonna print out enter first  number  basically we’re prompting them to enter  in the first number and then what i want  to do is i want to take whatever number  they enter and i want to store it inside  of a variable so i’m going to create a  variable called num1 and i’m just going  to set it equal to gets dot  chomp and this will basically just get  the number that they enter in and remove  the new line that gets entered when we  click the enter key  so now that we’ve done this we can do  something similar for the other two  pieces of information we need the next  the next thing i want to get is the  operator so i’m just going to say enter  operator  and basically this is going to be like  plus minus  division or multiplication they’re going  to enter in whatever they want to do so  over here instead of saying num1 we can  just say op and this is going to stand  for operator and we’re just going to get  whatever they input finally i’m going to  ask them to enter the second number so  i’ll say enter second number and we’re  just going to store this as num 2. so  essentially what we’re doing is we’re  asking them to enter in the first number  once they do that we’re asking them to  enter in the operator plus minus  division multiplication subtraction  whatever  then we’re asking them to enter in the  second number and we’re storing all of  that information inside of variables now  there’s one more thing we have to do  remember when the user enters in a  number ins when we ask them for input it  automatically gets converted  into a string so what we want to do is  want to convert the number that they  enter from a string into a floating  point number so i’m just going to say  gets.chomp.2 underscore f and this is  going to convert it into a floating  point number i’m going to do the same  thing down here dot 2 underscore f so  now num1 and num2 are going to be  floating point numbers assuming the user  entered the number in correctly all  right so once we do that we actually  need to do a couple other things so the  first thing we need to do is we need to  figure out if they want us to add the  numbers subtract the numbers multiply  the numbers or divide the numbers right  we have no way of doing that right now  so we need to figure out how can we  figure out if they entered in a plus  sign how can we figure out if they  entered in a minus sign we need some way  of figuring out what they entered and  this is a perfect scenario where we can  use an if statement remember an if  statement allows us to respond to the  different values in our programs so if  something has a certain value we can do  something it has another value we can do  something else  perfect situation right here for an if  statement we can check to see if it’s a  plus sign if it’s a minus sign if it’s a  division sign depending on the one it is  we can do something down here i’m going  to create an if statement i’m just going  to say if and then we always want to  make sure that we end off the if  statement down here and up here we’re  going to put a condition i’m going to be  checking a couple different conditions  in this if block the first thing i’m  going to do is check to see if it’s  equal to a plus sign so i can check to  see if  op and remember op was the operator that  got entered is equal to i’m going to  make those double equals and over here  we’ll just make a plus sign so this is  going to be true if the operator they  entered was a plus sign so down here  what we can do is we can just  say puts and we’ll just print out the  answer so i’m just going to put  num1 plus num2  awesome  but there’s also some other scenarios  right the scenario where they enter a  minus sign so i can say elsif elsif and  i’m going to say op is equal to  minus sign if that’s true we’re just  going to put  num1 minus num2  we can do the same thing for  multiplication and division so i’m going  to go ahead and copy this this will just  make it go by a little quicker  and now instead of checking for  subtraction we’ll check for division  which is just going to be a forward  slash finally we can check for  multiplication so i’m going to come down  here  and we’ll just check for an asterisk and  then down here we’ll multiply them  so  we’re able to basically capture the four  types of arithmetic right and respond to  them appropriately there’s going to be  one more situation though that we want  to cover which is if they enter in an  operator that’s not valid so i’m just  going to say else and remember the code  inside of this else is only going to get  executed when none of the stuff up here  is true so when none of this stuff is  true in other words if it’s not plus  sign minus division or multiplication we  can just print out an error we can say  puts  invalid operator  and that’s basically going to give them  a little error message like hey you  messed up you didn’t put in the right  operator so we’ve essentially written  out our program we get the three pieces  of information we check to see what  operator they put in and we respond to  it so let’s run our program and see how  it works now remember whenever we’re  running a program where we need to get  input from the user inside of ruby we’re  going to have to use the command line so  normally in this course we’ve been using  this little atom plugin over here called  atom runner but now we’re going to have  to use the command line so i’m going to  come down here and i just have one  inside of my atom text editor  and i’m just going to type ruby  draft.rb that’s the name of my file and  i’m just going to click enter  and let’s go ahead and use this little  calculator so it says enter the first  number we’ll enter in a 5.  and it says enter the operator so why  don’t we enter in a plus sign and it  says enter the second number so let’s  enter in a six when i click enter we  should hopefully get the correct answer  so you can see down here five plus six  is eleven so our program actually worked  that’s awesome all right let’s do it  again let’s try another one  why don’t we try to do some  multiplication so enter the first number  we’ll enter into five and then we’ll  multiply it by  8.65 and let’s see oh wait sorry this is  the operator so multiplication we’ll  multiply it by  8.65 and let’s see what we get 42.25 so  yeah that seems about right so  our calculator is functioning correctly  and let’s try one more case where we  enter an invalid operator so i’m going  to run the program one more time  enter in a four and then we’ll just  enter in like a t as the operator and a  five  and okay so it tells us invalid operator  so we have a four function calculator  we’ve actually built an awesome four  function calculator what’s cool about  this calculator is it’s able to respond  to the operator that the user enters and  we can do that using if statements so  this is one of those situations where if  statements are just going to come in  such handy because they’re so useful  right we can check all these different  conditions if one of them is true we can  do something if another one’s true we  can do something else  so this is kind of bringing together  like getting user input and if  statements into one single program which  ends up being a pretty awesome  calculator  in this tutorial i want to talk to you  guys about case expressions in ruby a  case expression is actually just a  special type of if statement that we can  use to check a bunch of different  conditions so we could use an if  statement to check you know to see if a  bunch of different things are equal to  other things or to check the values of  different variables but sometimes when  there’s a bunch of different things that  we need to check we can use something  called a case expression and it actually  makes it a lot easier so we’re going to  be creating a method in this tutorial  which is going to map day abbreviations  to day names so for example each day of  the week has an abbreviation like monday  is m-o-n and that gets mapped to monday  right  tuesday is tue and that gets mapped to  tuesday  wednesday is wed that gets mapped to  wednesday etc right  the abbreviations get mapped to actual  full days of the week so i want to write  a method that can take as a parameter  one input and it’s going to be an  abbreviation and it’s going to take that  abbreviation and it’s going to spit out  the actual name of the day  so if we pass in an mon it’ll spit out  monday if we pass in a fri it’ll spit  out friday right and that’s basically  going to allow us to convert these  abbreviations into the actual days of  the week so let’s go ahead and do that  i’m just going to define this  method so i’m just going to say def  and we need to also include an end over  here and a method name so why don’t we  call our method  get  day  name this is going to be called get day  name and inside of here we’re going to  pass the abbreviation so i’m just going  to call this day and day is going to  represent the  three-letter abbreviation of the  specific day right makes sense all right  so down here what we need to do is we  need to convert day  into the actual day name so the first  thing i’m going to do is create a  variable and we’ll just call it day name  and we’re going to store whatever the  day name ends up being inside of this  variable so i’m just going to set this  initially equal to the empty string  and then down here right before we end  off the method i’m going to return day  name  so our mission inside of this method now  is to  give this day name variable the value of  the correct day right we need to give it  the name of the day that corresponds to  this abbreviation so  one way that we could do this would be  using an if statement right so i could  come down here and you know hopefully at  this point you guys are familiar with if  statements i could say like if  day  is equal to mon right  if day is equal to mon then we can set  the day name  equal to monday makes sense  but there’s also a bunch of other  conditions we have to check so if that’s  not true we can check to see if the day  is equal to tuesday and if that’s true  then we can set the day name equal to  tuesday so for each day of the week i  can make it different else if right say  else if dayz will do wednesday thursday  friday saturday  but here’s the problem this is gonna get  really tedious and really messy really  fast right having to create seven  different if statements and you know  seven different else blocks checking  seven different conditions is gonna be  really difficult and tedious and that’s  why ruby has something called the case  expression and essentially what the case  expression does is it allows us to take  a scenario like this where we’re  comparing the same value to a bunch of  different values and put it into a  structure of its own so in this  situation in every single one of these  conditions we’re comparing the day  variable equal to another value right in  this condition we’re comparing it to  monday down here we’re comparing it to  tuesday we could also compare it to  wednesday thursday friday it’s the same  comparison with a different value in a  situation like that case expressions are  golden they’re going to help us out so  much so i’m going to get rid of this if  block and over here we’re going to set  up a case expression so the way that i  do this is just by typing out case  and now i need to give this  something so we’re going to give this  the value that we want to compare to a  bunch of different values in our case  it’s just day  right because i want to compare day to  month i want to compare data 2  like t-u-e i want to compare it to  w-e-d-t-h-u  i want to compare to all the day  abbreviations  so we’re going to need to end this off  now inside of here i can create  something called a when statement and a  when statement will basically do  something when  day is equal to a certain value so i can  say when and now i can compare day to  something so i could say when mon  and basically what me this means is when  day is equal to mon then down here we’re  going to do something so i can just put  some code down here so what i want to do  is set day name  equal to monday because now we know  they’re trying to get monday i can make  another one so i could say when  t-u-e  we could say day name  is equal to  tuesday right  and so basically what we’re doing is  we’re checking to see if day is equal to  monday and if it is then we’ll do this  if day  is equal to tuesday then we’ll do this  etc so i could make one of these for  every single day of the week and that’s  exactly what i’m going to do so i’m  going to go ahead and do that and then  we’ll meet back here and we’ll talk  about what happened  all right so i went ahead and created  one of these for every day of the week  so we have one two three four five six  seven of these here and each one of  these is doing something when a  different condition is met so when  it’s wet we’re setting day name to  wednesday when it’s fri we’re setting  day name to friday et cetera so  essentially we’re able to create like a  complex if else  structure but without having to like  actually write all of it out we can just  say like when  and it’s automatically going to check  this value with the value up here  so this can be really useful but there’s  actually one thing that we want to  account for there’s going to be  situations where the user enters in an  invalid day abbreviation so if the user  enters in a valid abbreviation like  s-u-n or s-a-t  we can cover that right but what happens  if they enter in something invalid so  what happens if they pass  a parameter  into this get day name method that isn’t  an actual valid abbreviation  well we can use something called the  else keyword and the else keyword is a  lot like the else keyword and if  statements i can just say else  and now anything that i put in here is  going to execute  when none of these guys up here are true  so i could say down here else day name  is equal to  invalid  abbreviation right so i’m basically  telling them hey you put an invalid  abbreviation in here so this is going to  get executed when there isn’t a valid  abbreviation all right so essentially  what i’m doing is in each of these  scenarios i’m giving day name a  different value then down here at the  bottom of our method i’m just returning  day name so this is a fully functional  method hopefully everything works why  don’t we come down here and test it so  i’m going to go ahead and just print out  the answer that we get back so i’m going  to say puts  and then it’s just called get  day name  [Music]  and we want to pass this an abbreviation  so i’m going to pass this mon for monday  right  and when i pass it this and we run our  program  you’ll see over here we print out monday  so our program is giving us back the  correct answer if i typed in s-a-t down  here then we should get back saturday  just like that if i typed in th you then  we’re going to get back thursday so you  can see we’re getting back the correct  value if i typed in something invalid so  if i typed in like dog like dog  now it should yell at us so it’s going  to say invalid abbreviation  and we were able to do this whole thing  while keeping it nice and clean and nice  and simple like this whole thing only  took me you know less than a minute to  write out  and i was covering all of these  different situations so this can be  extremely useful when you want to check  a single value like a day against a  bunch of different values and do  different things depending on the  situation so  definitely consider using case  expressions here’s the thing these  aren’t going to be appropriate in every  situation these are appropriate for one  specific situation right where we’re  checking the same value against a bunch  of different values but that situation  is very common so when it does come up  don’t be afraid to use case statements  they’ll make your life a lot easier  in this tutorial i want to talk to you  guys about working with while loops in  ruby  so a while loop is basically a structure  that we can use inside of our ruby  programs which will allow us to loop  through a particular block of code a  specific number of times so i can  basically like write out my while loop  and i can put some code in there and as  long as a certain condition remains true  i’ll keep looping over and executing  that same code inside that while loop  there’s a lot of situations in ruby  where we’re going to want to use loops  we’re going to want to do something  continuously until we need to stop and  so while loops are a great way to do  that and in this tutorial i just want to  give you guys a  broad overview of while loops we’ll look  at like the most basic type of while  loop you can write we’ll just talk about  the core concepts behind them so over  here i can actually create a while loop  and the first thing i want to do is  actually make a variable and this isn’t  you know 100 necessary when working with  while loops but you’ll see how this  variable comes in in a second so i’m  just going to call the variable index  and i’m going to set it equal to one  so we have a variable index and it has a  value of one and down here i can  actually create my while loop so the  first thing i’m going to do is just type  out while and then what i need to do is  i need to specify a condition and you  always want to make sure that you end  these off so i’m just going to put an  end down here  now here’s basically how while loops  work all of the code in between while  and end so all the code inside of here  that we’re going to put inside there is  going to get continually executed so  it’s going to keep getting looped over  and keep getting executed as long as  this condition is true  so as long as the condition that we  specify right there is true then we’re  going to keep executing the code inside  of this loop so over here i can actually  specify a condition so i’m going to say  while index  is less than or equal to 5. so  essentially what i’m saying is while  index is less than or equal to 5 i’m  going to keep looping through the code  inside of here and actually what i can  put in here we can just put you know  whatever code we want to keep looping  over so the first thing i’m going to do  is i’m going to type puts  and i’m going to print out the value of  index so i’m going to say puts index and  then down below here i’m actually going  to increment the value inside of index  so i’m going to say index is equal to  index  plus 1. so basically every time through  this loop i’m printing out the index  and then down here i’m incrementing the  index and actually i want to show you  guys something that’s cool we can do in  ruby i have here index is equal to index  plus one  but i can actually instead of saying  that i can just say index plus equals  one and whenever i say plus equals you  can also use minus equals  as well this is basically just going to  add the number over here to index so  this is going to add 1 to index so  that’s kind of a little shorthand we can  use in ruby  but back to the while loop this is gonna  keep looping through the code inside of  here as long as the index is less than  or equal to five so  let’s think about what’s going to happen  here actually let me just show you guys  so why don’t we execute this code i’m  gonna run the program and you’ll see  over here i’m actually printing out  values between 1 and 5. so i’m printing  out 1 2 3 4 and 5.  so maybe this is what you expected to  happen maybe it’s not what you expected  to happen either way i’m gonna walk you  guys through exactly what happened so  over here it says while index is less  than or equal to five so remember we’re  going to keep looping through this loop  as long as that’s true  so here’s how the execution of this  program goes ruby creates this index  variable gives it a value of one the  first thing ruby does when it gets to  this while loop is it checks this  condition if this condition’s true then  it’s gonna start looping through so in  our case this condition is true right so  ruby’s going to go ahead it’s going to  print out the index and it’s going to  add one to it once ruby has finished  executing the code inside the while loop  the next thing it’s going to do is go  back up here to the top of the while  loop and it’s going to check this  condition again  so every time ruby goes through this  loop every time it loops through the  code right it’s going to go back up and  it’s going to check the condition again  so every time through the loop we’re  constantly checking the condition so in  this case we incremented i we  incremented the index so the index is  now equal to two which is less than or  equal to five so we’re good to go right  the condition’s true so now we’ll  execute all of the code inside of here  again  and then again we’re going to come back  up and check the condition so every  single time we’re going we’re checking  that condition  eventually  index is going to be equal to 6 right  we’re going to get to a point where  you know index was equal to 5 so we  printed it out and then we added 1 to it  so now index is equal to six  we’re going to loop all the way back up  here and suddenly  this condition is going to be false and  when this condition’s false we’re going  to break out of the loop so we’re not  going to execute it anymore and we’re  just going to move on to the next line  of code so that’s basically how while  loops work we specify a condition as  long as that condition’s true we move on  and that’s how we can get this output  right here so for example if i was to  change this to like eight now we’re  going to print out one through eight  because the condition is only false when  we get to nine right so we’re printing  out eight times essentially so this is  extremely useful there’s tons of  situations where we’re going to want to  use while loops like this now i want to  point out one potential problem that  you’re going to have with while loops  and it’s basically called an infinite  loop  and an infinite loop is a situation  where the condition up here never  becomes false so it just loops through  infinitely and this is something that  could actually mess up your programs  quite a bit so you always want to make  sure that when you’re writing a while  loop that eventually the condition up  here is going to be false now in certain  special circumstances you actually want  to use an infinite loop but i’d say for  most cases especially as a beginner  you’re not going to want an infinite  loop so it’s just something to keep in  mind  but this is where the basics of a while  we’re specifying a condition as long as  that condition is true  we’re going gonna keep looping through  whatever code is inside of here so like  i said there’s tons of ways for us to  use this and in the next lesson we’re  actually gonna build a program where  we’re gonna use a while loop in order to  build a little game so stick around for  that that’s gonna be pretty fun  in this tutorial i’m going to talk to  you guys about building a guessing game  in ruby so we’re actually going to build  a little game and it’s basically a game  where the user can try to guess a secret  word so we’ll continually prompt the  user to guess a secret word and if they  get it right then they’ll get a success  message like hey you got the word right  if they get it wrong then we’ll let them  keep guessing so they’ll be able to keep  guessing  what the word is until they get it right  so this should be kind of cool and this  will show us how we can use something  like a while loop  in order to  build a little game so down here first  thing i want to do is create a couple  variables the first variable i want to  create is going to be the word that the  user has to guess so remember we’re  creating a guessing game so we need to  store a variable that has the word in it  right so i’m going to say  secret underscore word and this is just  going to store that secret word and i’m  just going to set this equal to  draft so you know this could really be  anything you want whatever the secret  word is going to be for our game and  finally i want to create one more  variable this is going to be called  guess and we’re just going to set guess  equal to the empty string for now so i  have two variables secret word and guess  and now that i have these two variables  i basically want to create a while loop  and i’m going to create a while loop  that’s going to continually loop through  and ask the user for input  so i’m just going to say while  and over here we need to specify a  condition  so again this while loop is going to  keep asking the user to input the secret  word and as long as they don’t get the  secret word right we’re going to keep  asking them so i’m basically going to  say while  guess  is not equal to  secret word  so as long as the user’s guess  isn’t equal to the secret word we’re  going to keep looping so down here what  i want to do is print out a prompt so  i’m just going to say puts  and we’re just going to say enter  your  guess  so this is going to prompt the user to  enter a guess  and what we want to do now is we want to  store whatever the user guesses  inside of this guess variable so i’m  just going to say guess  is equal to  gets.chomp  and remember chomp is basically just  going to get rid of that new line  at the end of  whatever the user enters so essentially  what’s happening here is while they the  user hasn’t guessed correctly while  their guess isn’t equal to the secret  word we’re just gonna prompt them again  for the guess and the guess is just  gonna get equal to whatever the user  types in so eventually what’s gonna  happen is they’re going to guess the  word correctly and when they do guess  the word correctly then this condition  is going to be false  so we’re going to come down here and  we’ll basically just print out a success  message so i’ll just say like  puts u1  all right so this is a pretty simple  program but you’ll see this actually  allows us to build a guessing game  inside of ruby so whenever we are  getting input from the user in ruby we  always want to do it through the  terminals or through the command line so  i’m going to go ahead and open up my  command line down here and i’m going to  run our program so it’s just called  draft.rb so i’m just going to type ruby  draft.rb and this will start executing  our program so down here you can see  it’s asking us to enter a guess so i’m  going to come down here and i’ll enter a  guess  so why don’t we just enter in some  nonsense so you’ll notice as long as i  don’t guess the word correctly as long  as i’m not entering in the correct guess  the program keeps prompting me to enter  a guess right it keeps asking me to do  that but here’s the thing if i was to  come down here and actually  enter in the guest correctly so if i  typed in like  draft now we should get a success  message so when i click enter you’ll see  it says you won because  we entered in the secret word correctly  so obviously this is a very simple  program but it actually works pretty  well and it does everything that we want  it to do  this is a very simple game and one of  the problems with this game is that the  user gets unlimited guesses they can  keep guessing the word forever until  they get it and that’s not the makings  of a good game what would be better is  if we imposed some sort of a guess limit  on them so they can only guess the word  a certain number of times otherwise they  lose the game so let’s go ahead and see  if we can add that functionality into  our program in order to do that i’m  going to have to create a couple more  variables the first variable i want to  create is going to be called guess count  and guess count is going to basically  tell us how many times the user has  tried to guess the word i’m going to  create a variable guess underscore  account and i’m just going to set this  equal to 0 because initially the user  won’t have guests so the guest count  will just be zero  now down here  every time the user guesses i want to  increment the guest count right so down  here basically this code means that the  user has tried to guess the word so  every time they do that i want to  increment the guest count i just want to  say guess  count  plus equals one and this will add one  onto the guest count so every time they  guess we’re going to increment that and  i want to create two more variables the  first one is going to tell us how many  guesses the user can actually take so  it’s going to be called  guess limit and why don’t we just say  the user has three guesses so three  strikes and you’re out so if they can’t  guess the word in three tries then  they’re going to lose the game all right  finally i want to create one more  variable and this variable is going to  tell us whether or not the user is out  of guesses  so i’m just going to say out of  guesses  is equal to  false  because initially the user is going to  have three guesses right when the user  reaches their guess limit we’re going to  set this out of guesses variable equal  to false and then we’ll be able to tell  the user like hey you lost the game all  right so now that we’ve created these  variables i’m just going to come down  here and inside of this while loop we  want to modify a couple things the first  thing i want to do is every time we ask  the user to guess the word i want to  make sure that they’re not out of  guesses in other words i want to make  sure that they haven’t reached the guess  limit so i’m going to use an if  statement to do that inside this while  loop the first thing we’re going to do  before we do anything else is just say  if and we want to check to make sure  that the guest count is less than the  guess limit so i’m going to if  guess count  is less than  guess limit  in other words if the guest count is  less than the guess limit then we know  they have guesses left right we know  that they have a certain number of  guesses left and they should be able to  keep guessing the word so if this is  true  then i want to allow them to guess the  word so i’m just going to take all this  code and we’re going to move it up here  all right so if the guest count is less  than the guest limit if they have more  guesses then we’re going to  do all of this  otherwise though  in the situation where the guest count  isn’t less than the guess limit  then we know for a fact that the user is  out of guesses right if they that means  they have no more guesses so inside of  this else statement what i want to do is  i want to set that out of guesses  variable  equal to true  and basically this will tell our program  like hey the user’s done they’re out of  guesses right we checked to see if they  were eligible for another guess and if  they weren’t then we’re gonna say out of  guesses is equal to true because they  ran out of guesses  all right  there’s one more thing we have to do  inside of this while loop and we have to  actually modify the while condition so  over here you can see we have our little  condition and we’re going to keep  looping while the guess  is not equal to the secret word  but now that we’ve imposed the guess  limit there’s actually one more  situation that’s going to cause us to  stop looping which is when the user’s  out of guesses so i want to say  i want to keep looping as long as the  guess is not equal to the secret word  and  as long as the user is not out of  guesses so and  not  out  of guesses  so we’re going to keep looping through  this loop we’re going to keep asking the  user to input information we’re going to  keep doing all the code down here  as long as the guess is not equal to the  secret word and as long as the user is  not notice i’m using this exclamation  point here out of guesses right  so  now we have our while loop set up right  we’re able to loop through here as long  as the user has guesses left there’s one  more thing we have to do down here so  before we just put u1 right  before if the user exited out of that  while loop that means they guessed the  word correctly so we knew for a fact  that they won  but now that we’re imposing the guess  limit there’s actually two situations  where the user could exit out of the  loop the first situation is where they  got the word right in which case they  won the second situation though is that  they ran out of guesses and if they ran  out of guesses we don’t want to tell  them they won we want to tell them they  lost  so we can actually check to see which  situation caused that loop to end i can  just say if  and i want to check to see if they’re  out of  guesses  and if they are out of guesses then i’m  just gonna print out  you lose  but otherwise if they’re not out of  guesses that means that they were able  to guess the word correctly within the  certain number of guesses so they win so  this is our basic program and let’s go  ahead and test it and see if it works so  again i’m going to open up the terminal  and over here i’m just going to run the  program and actually looks like i’m  getting an error here yeah so i forgot  to put an end statement down here i  always forget to do that so always make  sure that you put an end statement down  here  and now we’ll be able to execute our  program correctly so here we go all  right so it says enter a guess so why  don’t we just try to lose the game so  i’m just gonna enter in one guess two  guesses now i’m on my third guess so  this is my last try if i don’t get the  secret word on this try i’m gonna lose  the game so we’re gonna not get it and  it’s gonna say you lose so the program  is smart enough to figure out that  not only did we run out of guesses but  also that we lost  so let’s try it now but we’ll try to win  the game so  over here i’m gonna make a random guess  another random guess  so now we’re on our last try it’s our  last chance to get the guess i’m going  to type in the secret word which is  draft  and now the program’s going to tell us  hey you won so we were able to win the  game because we guessed within the guest  limit so  that is basically this program i know  this is a lot of code so i’m just going  to step through with you guys one more  time just to kind of do a broad overview  of what we did that way you can get a  better idea of what’s going on so we  have these two variables secret word and  guess  and these two are pretty obvious right  the secret word stores the word that the  user needs to guess guess is gonna end  up storing what the user guesses each  time through that loop  right so we’re gonna keep looping  through this loop as long as the guess  is not equal to the secret word  and down here we’re basically going to  store whatever the user guesses inside  of this guess variable that was like the  basic game that we built then we added  these three new variables guest count  guess limit and out of guesses guest  count is basically going to tell us how  many times the user has guessed and so  every time the user guesses we’re going  to come down here we’re going to  increment it right  guess limit is going to tell us how many  times the user can guess in other words  how many guesses they have available to  them and you’ll see down here we’re  using this if statement so we’re  checking to see if the guess count is  less than the guess limit  if that’s true if they still have some  guesses left then we’re going to execute  all this code we’ll let them guess  otherwise though we’re gonna use this  out of guesses variable and we’re gonna  set it equal to true that’s gonna tell  our program that the user’s out of  guesses  up here we also added another condition  inside of our while loop so we’re saying  wow the guess isn’t equal to the secret  word  and while the user is not out of guesses  so that’s going to allow us to control  what happens in this loop then finally  down here we’re checking to see if  they’re out of guesses so down here  there was two situations the first  situation was when the user ran out of  guesses so they lost the game and if  that’s the case we’re just gonna print  out you lose otherwise we’ll print out  u1 so that’s the basics of building our  guessing game so now your homework is to  go off and build a guessing game of your  own  in this tutorial i want to talk to you  guys about four loops in ruby now a for  loop is basically a structure in ruby  that allows us to loop through a  specific collection so that could be a  collection like an array it could also  be a collection of numbers basically  allows us to loop through a connection a  collection and for each item inside the  collection we can do something  actually in this tutorial we’re not just  going to be talking about for loops  we’re going to be talking about a  broad range of different loops and  looping structures which we can use to  loop through different things  essentially in ruby there’s a lot of  different ways we can do the same thing  i’m going to be showing you guys a bunch  of different ways to do essentially the  same thing so the first thing i want to  show you guys is i have this array set  up up here and it’s basically just  called friends and it has a bunch of  items in it you know basically just a  list of someone’s friends and i want to  show you guys how we can use a for loop  to actually loop through all of the  items inside of this friends array now  remember if i wanted to access a  specific item i could just say friends  and then pass an index in so if i said  friends one this is going to print out  karen onto the screen right so that’s  kind of how i can access an individual  element  but i can also use something called a  for loop and i can say for  friend  in  friends  and then down here i also want to end  this off so i’m just going to put end  and inside of this little for loop i can  just say puts  friend and what this is going to do is  it’s actually going to print out all of  the friends  inside of this friends array so now when  i run this program you’ll see we’re  printing out all of the  values inside of that array  and essentially what i’m doing is i’m  saying 4 and i’m specifying the name of  a variable  and on every iteration of this for loop  this friend variable is going to  represent a different friend so on the  first iteration it’ll represent kevin on  the second iteration it’ll represent  karen etc and i could actually name this  whatever i wanted so i could just say  element and down here  i could print out element  and it doesn’t actually matter so it’s  all going to be the same so that’s a  really easy way that you could loop  through  all the elements in an array so for each  element inside the array you could  basically do something and really  friends could be this could refer to any  collection inside of ruby this is  basically just a general way to loop  through a collection of elements  so this can be really useful when we’re  dealing with arrays there’s also another  way that we could do essentially this  same thing so instead of having this for  loop i could just say friends  dot each and i can say do and then i  want to make it two like little vertical  bars right here and i’m just gonna pass  in the same thing as we did before like  a variable and this variable is going to  represent a specific friend on each  iteration of this loop so then down here  again we can just say end and over here  i can just print out  friend  and so now it’s doing the same thing as  we did before we’re printing out each  one of the friends that was inside of  that array and again i could this  doesn’t have to be friend this could be  anything i want so that’s basically how  we can loop through like an array a  couple different ways there’s also  another thing we could do we could loop  through a range of numbers so i could  specify that i want to go through a loop  and go through the code a specific  number of times so i could come down  here and i could say like  for index in let’s say zero to five  and i want to do something so i could  just print out the index this is  basically just going to loop through  this loop five times so you’ll see over  here it prints out actually i guess it  looped through six times so it prints  out zero one two three four  and then five so it’s gonna loop through  from zero to  five  and a lot of times in a lot of cases  you’re gonna want to  go through a loop a specific number of  times and you can do it just like that  there’s also another thing we could do  we could say like we could specify a  number here so i could say like six and  i could say dot times do  and inside of these vertical bars i can  just say like index  and again this doesn’t have to be called  index this can be called anything we  want  and here it’s the same thing so this is  going to loop through something six  times so over here we’re printing out  zero through five so these are all  basically different ways that we can  loop through other collections or we can  loop through something a specific number  of times in ruby and this can come in  handy a lot  in this tutorial i want to talk to you  guys about building an exponent method  in ruby one of the things i wanted to  show you guys was how we can use  something like a for loop in order to  write a little method and i think one  method that could be kind of cool to see  how we might be able to write it would  be an exponent method so an exponent  method would basically take two numbers  it would take a base number and it would  take a power number and then it would  basically take the base number to the  power number so  if we passed in like a two and a three  then this would basically give us like  two raised to the third power that’s  essentially like what this method’s  gonna do  and let’s go ahead and create it so i’m  just gonna say  def  and why don’t we just call this pow for  like power so it’ll take a number to a  specific power  and then inside of these parentheses we  actually want to have this method accept  two parameters as input so we want the  base num and we also want the  pound num  so the base number and that’s going to  be taken to the power number okay  all right let’s end off this method now  inside of here we need to figure out how  can we actually do this you know how can  we take the base number to the power  number and my solution would be to use a  for loop the first thing i’m going to do  actually is create a variable so i’m  just going to call this result and i’m  going to set this equal to 1.  and then down here right before  the end of the function we’re just going  to return it so i’ll just return result  so ultimately this variable result is  going to represent the value of the base  number taken to the power number so  let’s use a for loop essentially what i  want to do is i want to multiply the  base number by result  poundnum times that makes sense so let’s  flesh out this for loop and then we’ll  kind of see how it’s going to work so  i’m going to say this i’m going to say  pow num  dot times  do  and basically what we’re gonna do now is  we can actually just pass this in index  now i don’t know if we’re gonna actually  use this index but it might be good just  to pass it in anyway  and down here i’m just gonna end this  off inside of this little loop here  we’re basically just going to multiply  result times the base number so i’m just  going to say result  is equal to result times  base num  so essentially what’s happening is  pownum.times  do index this is essentially just going  to loop through the code inside of here  pownum times so if powernum is 3 we’re  going to loop through this code three  times if poundnum is 10 we’re going to  loop through this code 10 times and  every single time we go through this  code we’re going to multiply result  times the base num so with the first  time through the loop it’s just going to  be result which is 1 times base num so  result’s going to end up just being  based on second time through it’s and  going to end up being like base num  squared  third time through it’s going to end up  being like base num cube so  this is essentially how we can go ahead  and write this method now it’s important  to know that this is only going to work  for positive number exponents so if  poundnum is a negative number then this  method’s actually not going to work um  but just for our purposes in this  tutorial let’s just assume that we’re  always going to use a positive exponent  so once we’ve multiplied result times  the base num as many times as we need to  then we can just return it so believe it  or not this is actually all we need for  this method to work so let’s go ahead  and call this method and we’ll see what  we get so down here i’m actually just  gonna  print this out so i’ll just print out  pow and why don’t we take two  to the third power okay  so let’s run this and you can see over  here we get eight so two cubed is 8.  let’s try another one why don’t we do 4  cubed  so now we should get 64. good  let’s do  5 squared  so we should get 25 and we do so looks  like this method’s working so again this  is going to work for a positive number  exponents negative number exponents uh  this won’t handle it but for our  purposes this kind of demonstrates how  we can use a little for loop or in this  case we’re just saying poundnum.time  in order to loop through something a  certain number of times and this can be  extremely useful when we want to do  something like this and also i just want  to point out over here i had kind of  stored this variable index  but you don’t actually have to do that  and in this case we don’t need it inside  of this method but it’s not just good to  just have it there so that’s to kind of  give you an idea of like a situation  where we might be able to use one of  these looping structures in order to  like perform a function  in this tutorial i want to talk to you  guys about comments in ruby a comment is  basically a little line of code that  ruby is actually going to ignore so a  lot of times when we’re writing our ruby  programs there’s going to be situations  where we want to leave like little notes  or really just little comments inside of  our files and comment is exactly that  it’s just a little line of code a little  line of text that is meant to be read by  humans so if you’re writing a ruby file  you can leave a little comment and then  when you come back later you can read  the comment or another developer who’s  looking at the file can read the comment  and you can basically communicate  information that’s outside of just the  actual code in the file there’s a lot of  situations where comments are going to  be useful the basics of making a comment  is you essentially go to a line of code  and you can use this hashtag symbol and  anything that you put after this hashtag  symbol is going to be in the comments so  you notice that my text editor is  actually styling this a little bit  differently so if i was just to come  down here and type out some random text  you can see that this is actually going  to get rendered by the program but if i  put random text over here after this  hashtag this is going to be considered a  comment so i can basically write any  plain text over here  and when i run the program you’ll notice  that nothing gets printed out you know  that’s inside of this comment block so  like i said comments are really useful  for leaving little messages or little  reminders inside of your programs you  can also put comments after lines of  code so for example here we have just a  line of code if i wanted after this line  of code i could put my hashtag and i  could type a comment after here and this  is something that you’ll see a lot in  programs like people will include  comments either after lines of code or  directly above lines of code and a lot  of times people will use comments in  order to like describe what a line of  code is doing so i could say like this  line prints text so obviously you know  it’s pretty obvious what this line is  doing but if i wanted i could put a  comment there  another thing that comments are used for  is commenting out code so there’s going  to be certain situations when you’re  programming where you might want to run  your program but without a specific line  of code so maybe you have like this  whole you know big method and you want  to see  what your program would be like without  a specific line well one thing you could  do would be just to erase the line of  code right you could get rid of it and  you can go ahead you could run the file  and  you know basically the code’s gone but  the problem with that is you have to  physically delete the line of code  a better way would be just to come over  here and right before the line of code  we could put a comment there and this is  what’s called commenting out a line of  code and a lot of times developers will  use this to basically temporarily like  disable a line of code so now when i run  the program we’re not printing out  comments or fun anymore because it’s  commented out and the great thing is  when i want this line of code back i can  just get rid of the comment and we’re  back in business so you know really a  comment is just kind of like open for  business like whatever you want to do  with it whatever you you know you think  you can accomplish with a comment you  can go ahead and do that um a lot of  times people will also put comments on  multiple lines so you’ll see some people  they’ll have like you know  line one of a comment and then down here  they’ll make another hashtag they’ll  have line two  this is one way that you can put  comments on multiple lines and honestly  this would probably be the recommended  way like the most you know ruby-esque  way to do it there is another way that  we can do this though um so yeah like i  said normally with these hashtags like  it only applies to the the one line  right but if you wanted you could create  a comment block and all you have to do  is just say equals begin  and then wherever you want the comment  block to end you can just say equals end  and it’s important to know that these  can’t have any white space before them  so you see if i put waste white space  there it disables the comments so down  here you can just type whatever you want  inside the comment  and it’ll be rendered as a comment but  like i said i think  probably a cleaner way to do it is just  to put a hashtag on every line  especially you know if you’re moving to  other programming languages a lot of  other programming languages will do  stuff like this so you know it’s just  something to keep in mind one final  thought about comments  you want to use comments only when  they’re absolutely necessary  you know comments if you have too many  of them and they’re too active in the  document a lot of times they can be a  little bit distracting generally your  goal should be to write code that is as  readable and clean as possible so that  you don’t need comments to explain it  but a lot of times a comment can come in  handy so if there’s a situation where  you think you need a comment don’t be  afraid to put one in  in this tutorial i’m going to show you  the basics of reading from an external  file in ruby now one of the useful  things we can do with ruby is we can  actually read from external files that  are on our computer so if i had like a  file that was on my computer somewhere i  could actually read from that file  inside of my ruby program  so i’m going to show you guys how to do  that i actually created a text file over  here it’s in the same directory as this  draft.ruby file it’s called  employees.txt so i’m going to open that  up and you can see over here it’s just a  very simple text file of employees so  these might be like employees at an  office or you know whatever we have you  know the name and then we have the  position so jim is in sales andy’s in  sales creed is in quality assurance  basically it just says their names and  then it says what they do so their jobs  so this is a you know an example of  maybe a text file that you might want to  read from inside of your ruby files so  i’m going to show you guys how we can  read from this file over here in my  program i have to use a special command  so i’m just going to say  file  dot  open and now i’m going to make an open  and closed parentheses and so inside of  this open and close parentheses i want  to give this a couple different  parameters the first one is going to be  the name of the file in my case i have  this file stored in the same directory  as this ruby file so i can actually just  put in the name of the file so i can say  employees.txt  but if this isn’t in the same directory  as the ruby file you’re going to have to  include either a relative path in here  so for example if it was in a directory  called like files i could say forward  slash files  or you know files forward slash  or you’re gonna have to include an  absolute path that starts at your hard  drive so that would be if you’re on  windows it could be something like c  uh you know users whatever  so basically some sort of path to the  file or if it’s in the same directory  you can just put the file name we also  need one more parameter in here which is  going to be the mode that we want to  open the file in so when you open a file  in ruby you can actually open it in a  couple different modes and there’s maybe  like six or seven different modes that  you can open files in  the one we’re going to be using is  called read so i’m just going to type an  r in here and that’s going to tell ruby  that all we want to do is read the file  so we don’t want to modify it we don’t  want to write to it we don’t want to  overwrite it we just want to read it and  now what i can do is i can say do  and over here i can make two open  vertical bars and i’m just going to say  file and basically what this is doing is  it’s storing this file that we just  opened inside of this file variable so i  now have a variable called file which  will represent the file that we just  opened and basically what i want to do  is i want to come down here and  down below here i basically just want to  say end  so this is one way that we can open up a  file and i think for a beginner this is  probably the easiest way to do it so you  just say file.open the name of the file  the mode in our case are do and then  this is now going to store this file  inside of this variable so whenever we  want to do with this file we can do  inside of here and the reason that we  need this end block is because this end  block is going to signify when we’re  done working with the file  and when we’re done working with the  file it’s just going to go ahead and  close the file so we’re not going to be  using it anymore we’re not going to have  it open anymore so this is going to be  good just as a beginner just to kind of  you know play around with  working with and reading from files  so inside of here we can use this file  variable that we  created over here and we can actually do  some stuff so the first thing i could do  would be just to print this out so i  could uh just say puts file and now  you’ll see over here on the screen it’s  printing out like all of this  information this is essentially like  some like metadata about our file it’s  basically how it’s stored in ruby but if  we want we can read the file so i can  say puts file dot read and now we’re  going to get all the information in the  file so you see we’re printing out all  the different names all the different  employees from our file so read is  basically just like you’re reading the  entire file and if you want you could  use any sort of you know string methods  on this i mean this is basically just  giving us a string so i could say like  read dot  includes  or include and you know we could see if  like someone in there is named jim or  whatever and we get true back so you  know essentially this is just giving us  a string with all the information in the  file there’s also another uh thing which  we can do which is called read line and  read line will basically read a line  from the file so if i print out  file.readline it’s going to read the  first line of the file but what this is  actually going to do is it’s going to  read the first line and then it’s  basically going to say like okay now  we’re on the second line so let me show  you guys what that means so if i was to  copy this line and paste it down here  this is gonna print out the first line  of the file and then this one’s gonna  print out the next line in the file so  you’ll see  when i run this it prints out jim and  then andy so every time we read the line  we’re basically telling the file that we  want to move on to the next line so this  is a good way if you want to read like  each line individually you can also do  the same thing but with the characters  so i could say read char and then down  here i could also say read char  and instead of reading the individual  lines this is just going to read the  different characters inside the file one  by one so  now we’ll be able to print out jim yeah  so rechar and re-line can both be really  useful like as you go through a file  there’s one more i want to show you  which will allow us to loop through all  of the lines in the file and then for  each line in the file we could like do a  specific thing  so i can do is i could actually say for  line  in  file dot read lines  and read lines is a little method which  is basically just going to return an  array  of all of the lines so actually let me  show you guys what this is going to give  us i’m going to print this out  so i’m going to print out file.readlines  and you’ll see it’s just printing out  all the lines in the file but this is  actually stored as an array so i could  access like if i wanted the third line  in the file  then i’ll be able to access it like that  so we can loop through this array like  you would normally loop through an array  so we could say like for a line in file  dot  read lines  and down here we can just put some code  that we want to do for each line so i  mean obviously i could just print them  out so i could say put line  and this will print out every line but  in here we could modify the line we  could look through we could do anything  we wanted to each individual line in the  file  and here’s the thing you know  these are just sort of the basics you  know i mean this is everything that you  need to basically take a file you know a  text file loop through every line and  then you’re basically just given a  string  so this line over here i mean these are  all just strings in our program so you  know you can use all the different  string methods to parse through those  lines to figure out what they’re saying  get information from them  and you know really it’s honestly super  easy to actually read from a file so i  would say that’s all like the main you  know those are sort of like the most  popular the most uh commonly used  methods when we’re talking about files i  also do want to show you another way  that we can open a file so you’ll see  over here we said file.open the name of  the file and then we did this whole like  do file thing  there’s another way that we could  actually do this so instead of saying  this stuff over here i could actually  just store this in a variable so i could  say like file is equal to all of this  and we can do exactly the same stuff we  did before so i could say like  puts file.read and we’ll still be able  to print out all the contents of the  file the only thing with this though is  whenever you create a file like this you  always want to make sure that you close  the file when you’re done with it  closing the file basically means you’re  saying that you’re done with the file so  you don’t want to leave a bunch of files  like open and in memory so if you can  just say file.close  and this will go ahead and close the  file and you won’t have to worry about  it like taking up any more space in your  program but that’s really the basics and  you know you can really just sort of go  from here right practice playing around  reading different files practice reading  different types of files these can be  they don’t just have to be text files  they can be any type of file  in this tutorial i want to talk to you  guys about writing to files in ruby one  of the cool things about ruby is we can  read files but also we can write files  so i could actually write information  out to a file and i can do that right  inside my program so in the last  tutorial we talked a little bit about  reading files so we kind of opened up a  file here  we opened up employees.txt which was  just this text file that had a bunch of  like i don’t know employees and a  company in it  and  we basically stored that file inside  this file variable we could do all sorts  of stuff with it now i want to talk to  you guys about actually writing  information to files and the first thing  i want to kind of talk to you guys about  are the different file modes so  different ways that we can open up a  file you’ll see over here i’m passing in  two arguments to this open method i’m  passing in the name of the file  obviously but then over here i have this  little r and this is basically a file  mode so this kind of tells ruby like  when we’re opening the file it kind of  gives ruby an idea of like what we want  to be able to do with it right if i say  r this stands for read but there’s  actually a bunch of other ones so i’m  gonna head over here to my web browser  and i’m just on a stack overflow page  and i think this actually gives a pretty  good explanation of  all the different file modes in ruby so  you can see here r is basically just  read only starts at the beginning of the  file r plus is a read write  w is write only it truncates the  existing file  w plus is read write so there’s all  these different like file modes that we  can open up and this is a pretty cool  list you know  one thing that can be fun to do is just  play around with a file and opening it  in all these different modes just to  kind of see what they do in this  tutorial i’m going to be opening the  file in a couple of these different  modes and i think it’s going to be  pretty fun so the first thing i want to  show you guys is how to append to a file  so over here i have my little list of  employees right let’s say that i wanted  to add an employee onto this file and so  i didn’t want to modify any of the  existing employees i basically just  wanted to append one to the bottom of  the file so we want to add in a new  employee well i can open up this file in  what’s called the append mode so i’m  just going to put an a here and that  basically means all we can do to this  file is just add information onto the  end of it so we can’t read from it we  can’t write to it  well we can write to it but we can only  write at the end of the file so we can  just append something so down here what  i could do is i could say file dot right  and inside of a parentheses we can put  whatever we want to write to the file so  why don’t we try to add a new employee  so why don’t we say we’ll add oscar who  is  in accounting basically what’s going to  happen is when i run this program this  line of text is going to get appended  onto the end of the employees.txt file  so i’m going to go ahead and run this  and now over here in the employees.txt  file you’ll see we have this oscar from  accounting line but it actually just got  appended right onto the end of the file  so literally right after the last  character  and i want to show you guys what we can  do to mitigate that so i’m just going to  get rid of that and we’re going to run  this again this time though i’m going to  run it with a new line in front and  basically what this will do is it’ll go  on to the next line and then it’ll print  oscar from accounting so now when i run  the program we should get what we want  which is this so oscar is just on the  next line  here’s the thing when you’re appending  to a file or really when you’re writing  to a file you need to be careful because  if i was to run this program again so  i’m just going to run it again and i go  back over to this employees.txt file  you’ll notice that oscar got appended to  the end of the file again right so every  time you run your program it’s going to  keep modifying the file so you need to  be careful whenever you’re writing to  files because you’re modifying the  actual file so if you you know mess up  and you run your program one too many  times then your files are basically  going to be you know if not ruined  they’re going to have information in  there that you didn’t want so that is  the basics of appending you can just  append something onto the end of the  file it’s super simple super  straightforward and that can be really  useful um  in a lot of different scenarios another  thing we can do is just write to the  file so instead of a i’m going to say w  here and this basically gives us the  ability to  modify information in a file overwrite  information in a file and also create  new files which is pretty cool  so over here if i was to say file.right  and i just did oscar accounting now what  this is going to do is it’s going to  overwrite this entire employees.txt file  so let’s go ahead and do that i’m going  to run this and you’ll see over here  this is actually overwritten the entire  file so we’ve overwritten everything in  this file and that’s basically what  happens when we write to a file  so i’m actually going to undo this and  we’ll bring this back to how it was i  want to show you guys another thing we  can do which is when you’re writing to a  file  you can actually create new files so for  example let’s say i wanted to create a  new file  i could actually come up here put the  name of the new file that i wanted to  create so maybe i want to create like an  html file i could call it index.html  down here i could actually write out  some html into this new file so i could  say file.right  and we can just put some i mean we can  put like html you know whatever  so html is like another sort of  programming language and now what’s  going to happen is when i run this  program  a new file called index.html is going to  get created and this is actually going  to get written into that file so you’ll  see over here  index.html has been created and it has a  header one and it’s just saying hello  that’s pretty cool you can create all  different types of files you can use all  different types of file extensions over  here and that’s you know a really useful  way to create files another thing i want  to show you guys is another file mode so  instead of just writing we’re actually  going to read and write so i’m just  going to say r plus  basically what this means is we can read  the file and we can write the file and  when we’re doing stuff like this it  makes it really useful so okay so we  have all of our employees here  i can actually use some of the read  methods that we learned in the last  tutorial and some of the right methods  in order to write information at  specific points in the file  so for example we could insert text at a  specific point in the file so i could  say like  file.readline  and this is basically going to move to  the next line in the file so whenever  you’re reading a file there’s like  something called a file cursor  or you’ll hear people call it like the  the file buffer sometimes basically like  it when we open the file we start at a  specific place in the file so if you  come back over here  you’ll see like read read only starts at  beginning of file right r plus read  write starts at the beginning of the  file so in certain times when we use  these certain modes they’ll start at  certain positions in the file right a  little cursor or whatever  when i say file.readline that moves that  little cursor to the next line in the  file  right and so now  essentially where this cursor is is at  the second line of the file so i can  come over here and i can say file.right  and i can basically say like  overridden  and now when i run this and i go over to  my file and actually we gotta change  this back to employees.txt my bad  so when i go back over to my file  you’ll see that it says overridden down  here right so i was able to override the  text that was on that second line i  could even do the same just for parts of  it so if i just wrote out like  high down here  you’ll see that high is going to get  inserted  only in these first two positions right  so you’re just overriding like the  positions  right after that little cursor and so  that can be really you know useful and  really you can use this read line and  there’s also another one called read  char and read char will just read like  an individual character so it’ll move  that cursor like one character you can  use these different functions to move  that cursor around the file and insert  text you know in very specific places  and that you know you can just kind of  play around with that to be able to do  stuff like that but i would say those  are kind of the basics you know we learn  how to create new files we learned how  to override existing files we learned  how to insert text into specific places  and specific files  we learned about the different modes so  again you know go off research some of  the different modes where you can open a  file but i would say really writing to  files is extremely useful and there’s a  lot a lot of programs that do stuff like  this and rely on stuff like this  in this tutorial i want to talk to you  guys about exception handling in ruby  now sometimes when you’re writing your  ruby programs you’re going to come  across certain errors so a lot of times  when we get an error in ruby it’ll crash  our program in fact every time we get an  error in ruby it’s going to crash our  program and sometimes when you’re  writing ruby code that you want to be  able to execute for you know months and  months or even like a couple years at a  time you want to make sure that you’re  able to handle any errors or exceptions  that pop up in your program and i’m  going to show you guys how you can do  that today using something called a  rescue  so let’s go ahead and trigger an error  in our program one of the easiest ways  to do this is to divide something by  zero so if you’re familiar with uh you  know math you can’t actually divide  something by zero so if i said like  num is equal to 10 divided by zero  when i run this program you’ll see over  here we’re getting a bunch of red we’re  getting an error it says divided by zero  zero division error so ruby’s telling us  you had a zero division error right  another error that we could use and i’m  actually just going to comment this guy  out right here would be  trying to access an invalid index in an  array so we could try to access an array  index using something like a string so i  just have this array over here of lucky  numbers if i was to say like lucky  nums like 0 this is going to be fine  this will give me this 4. but if i said  lucky numbs dog  i can’t actually pass a string into here  so i’m going to get another error and  over here it says no implicit implicit  conversion of string into integer so i’m  getting a type error  and there’s a bunch of these different  errors in ruby you know that handle a  bunch of different situations going  wrong but the point is that if a  situation like this comes up in my  program a lot of times i’m gonna want it  to not break the program like i’m gonna  not want it to explode the program and  then you know the program is done  running so we can actually watch out for  some of these common errors and we can  do what’s called catching them and when  we catch an error or we catch an  exception that means we’re basically  saying to the program like hey we know  something went wrong but we’re handling  it and it’s all good don’t worry about  it you don’t need to crash and burn so  i’m going to show you guys how you can  do that really the most basic way to do  it is  just to use something called rescue and  begin tags so over here i’m just going  to say begin and down below here i’m  going to say  rescue  and then down here i’m going to say end  so this is the basic structure  any code that i think is gonna throw  an error or an exception in my program i  want to put it inside of these this  little begin block so for example this  division by zero let’s grab this and we  can put this in here as long as this  code like the code that we think might  break is inside of that begin block then  if something does go wrong here then  it’s basically just going to go down to  the code inside this rescue block and  execute that so i can say down here like  puts division by zero error  so now when i run my program instead of  the program just exploding  it’s gonna say division by zero error so  the program actually didn’t break the  program didn’t stop executing our  program just handled the error and  defaulted down here and basically  printed out hey there is a division by  zero error here’s the thing the same  thing is going to work for these lucky  numbers so if i was to grab this lucky  numbers and i put this over here and i’m  actually going to comment this out  now when i run this program you’ll see  that the lucky numbers is going to do  the same thing so over here it says  division by zero error  here’s a problem though let’s say i have  more than one piece of code inside of  this begin block that has the potential  to break the program  right so these two blocks of code will  break the program right we we know for a  fact that they will but let’s say that  they’ll only break the program some of  the time right so maybe we have a  program where the user can enter in two  numbers to divide and sometimes they’re  going to enter into 0 in which case  we’re going to have to handle this error  and maybe sometimes they won’t the  problem is that if i just put division  by zero error down here this is just  going to catch any error that gets  thrown so it’ll catch this error up here  it’ll also catch this error and remember  those are two different types of error  one was a division by zero error one was  a type error so there’s actually a way  that we can specify specific rescue  blocks for specific types of errors so  for example that division by zero if i  wanted i could come down here and i  could say rescue and then i can just  type out zero  division error  and remember this is basically the error  that got thrown when we divided by zero  and so here’s the thing when i run this  num ten divided by zero thing it’s going  to get caught by this division by zero  error when i run the program  but if i was to run this lucky numbers  so if i just uncommented this  this isn’t going to get caught so this  is still going to break the program  you’ll see over here we’re still getting  a type error so actually what i could do  is i could create another rescue block  for that specific type of error  and we could just say  type error  and then down here inside of this rescue  block we could you know print out a  message like  wrong type  so now this lucky numbers with the dog  index is going to get caught and it’s  going to say wrong type so a lot of  times in ruby you’re going to want to be  specific about what errors you want to  catch and so basically what this means  is in the case of a zero division error  i could do something i could put a bunch  of code inside of here that’s going to  do something in the case of a type error  i could do something else another thing  we can do is we can actually take the  error that got thrown and we can store  it in a variable so i could say type  error and equal sign and greater than  sign and then i could just type in the  name of a variable where i want to store  the exception that got thrown so i’m  just going to call it e  and down here i could actually print out  e  so when this type error gets called  instead of just typing out hey wrong  type it’s going to tell us what the  actual error was so it says no implicit  conversion of string into integer and a  lot of times this can be useful because  different situations will cause this  type error to  run in other words like different stuff  up here might cause the type error so by  storing the error inside of a variable  called e and printing it out we can tell  the user exactly what went wrong so  that’s pretty useful and this is  optional you don’t have to do this but a  lot of times you’re going to want to and  it’s usually a good practice if you’re  writing a script or a piece of code  that’s going to be running for long  periods of time for example if you need  a piece of code to be running for like a  couple months or like even a couple  years at a time like on some server  somewhere  using these rescue tags is extremely  useful because you’ll basically prevent  your program from terminating or prevent  your program from blowing up when stuff  goes wrong  and just one more thought on this and  you know different people have different  opinions about this but generally it’s a  good idea to specify the specific errors  that you want to catch  it’s usually not a good idea just to use  rescue the problem is because this is  just going to catch any error under the  sun so a lot of times like you’re going  to want to be able to respond to  individual errors differently so this is  kind of like almost too broad in a sense  because you’re just casting a huge net  and catching every error in your entire  program but if you want to use that then  it’s available to you so that’s the  basics of using those begin and rescue  tags in order to  stop our programs from blowing up  in this tutorial i want to talk to you  guys about classes and objects in ruby  now classes and objects are a extremely  important concept in ruby so in this  video i’m going to be giving you a basic  overview of what a class is what an  object is how we can create classes and  how we can create objects so make sure  you pay attention because this is a very  important topic when it comes to ruby  now ruby is an awesome language because  it allows us to use all different types  of data inside of our programs so if you  watch the video that i did on data types  in ruby we looked at some of the basic  data types that ruby gives us things  like strings  integers floating point numbers things  like booleans  and even like the nil value right  there’s all these different types of  data that we can represent and we can  work with and we can use  in our ruby programs but here’s the  problem is not all things not all  information not all objects not all  entities in the real world can be  represented by just those pieces of  information right you can’t represent  everything with just a string or with  just a number or with just a boolean  right there’s a lot of things in the  real world like a lot of you know  different things like a person or a  phone or a credit card or a water bottle  or a computer a keyboard a mouse like  there’s a lot of these real world things  that can’t be  represented using just a string or just  a number one of the cool things about  ruby is it allows us to create our own  data types so i could actually create my  own data type that it would allow me to  represent like a phone inside of my  program or that would allow me to  represent a computer or a credit card or  a book or a lampshade or a dog really  anything i would want i could represent  inside of my program and i could  essentially just create my own data type  and essentially what that is it’s called  a class so we can create a class in ruby  and a class is basically just a custom  data type so it’s a data type that we  can define so i can basically say like i  want to represent a book inside of my  program and i can say here’s what a book  looks like in my program maybe it has  like a title it has an author it has  like you know a certain number of pages  associated to it and then i can take  that book data type and i can create  individual books from there so i can  take this book class that we’re going to  create and i can create individual books  i can represent individual books inside  of my program so i’m going to show you  guys how we can do that we’re going to  create a book data type or a book class  and then we’re going to create some book  objects this is going to be pretty cool  in order to create a class i can just  come over to my ruby file and i’m just  going to type out class  and now i want to give it a name and  generally in ruby when we create a class  you’re going to want to give it a name  with a capital letter so i could say  like  b-o-o-k with capital b  and then i’m going to come down here and  just type end  so remember  a class is essentially a custom data  type in ruby so when we create a class  we’re basically modeling a real world  entity or a real world object inside of  our program so inside of this book class  i’m basically telling ruby what a book  is i’m defining like here’s what a book  is and generally when we create a class  we’re going to give these classes  various attributes and that’s how we can  create a data type i can say okay here’s  my book class and every book is going to  have a title it’s going to have an  author and it’s going to have a number  of pages right a class is essentially  like a blueprint or a template for a  specific entity or object in the real  world like a book is an actual entity in  the real world right a book is an object  that we can interact with and work with  and this class is a template or a  blueprint for representing a book inside  of our program we’re basically creating  our own data type  so inside of this book class i can  actually define a bunch of attributes  and attributes is just going to be  information that all books are going to  have so we’re essentially telling ruby  like hey all books should have the  following information so inside of this  class i’m just going to type out attr  underscore a-c-c-e-s-s-o-r  attribute accessor  and now i’m going to make a space and  over here i’m going to type in the  attributes that a book should have  inside of our program so i’m just going  to type a colon and i’m going to type  the name of the attribute i want to  define  so all books should have a title i’m  going to put a comma  book should have an author  and then books should also have a number  of pages so i’m just going to say pages  so essentially what i’m doing here is  again i’m laying out the blueprint for a  book inside of my program so i’m  essentially saying like here’s the book  data type and all books are going to  have a title they’re going to have an  author and they’re going to have pages  so this is like our overall blueprint  our overall template so now that we’ve  created this now that we’ve told ruby  what a book is we can actually go and  create individual books inside of our  programs and these are called objects  and an object is essentially just an  instance of a book or an instance of a  class so i could create a book object  and that book object would represent an  individual book in my program  so i’m going to come down here and you  can create objects essentially the same  way that you create variables and  actually as we’ll learn  all variables in ruby are actually just  objects so i’m going to give this a name  so we’ll just call this book one  and i’m going to set it equal to  book dot new i’m going to make an open  and close parentheses basically what  this is doing is it’s telling ruby that  we want to create a new book so we want  to create an actual book inside of our  program in other words we want to store  the book data type inside of this book  one variable  now down here what i can do is i can  actually give this book attributes so  remember we defined a book up here that  it’s going to have a title an author and  a number of pages so down here i can  basically say book one dot title is  equal to  and we can make this like a harry potter  book  right  so i’m basically defining what the title  of this book is and down here i could  say  book two dot  author and we’ll make the author  jk rowling  and we can say and actually whoops this  has to be book one i’m sorry  and then we can say book one dot pages  and we can set this equal to like i  don’t know maybe it has 400 pages or  something  so essentially what i’m doing is i’m  creating this book object and remember  an object is an instance of a class so  an a book object is basically just like  a given book inside of our program  that’s going to have all of these  attributes and i’m giving it all of  these different attributes i’m assigning  it some information so what i can  actually do now is i can interact with  this object so i could say puts  book1 dot title and this is going to  print out the title of book one onto the  screen you see over here it’s printing  out harry potter over here i could print  out book one dot pages and it’s gonna  print out all of the pages in book one  so there’s 400 and we could do the same  with the author so essentially what i  did was i created my own data type up  here i created a template for what a  book is inside of our program  i said the book is going to have these  certain attributes i created a new  instance of a book so i created a book  object and then i gave all of those  attributes specific information so i  said like the title of book one is going  to be harry potter the author of book  one is going to be jk rowling and what’s  cool about objects is we can create as  many of them as we want so so down here  i could actually create another book i  could say  book 2 is equal to  book.new  and then i can start giving this some  attributes so i could say like book two  dot title is equal to lord of the rings  you could say book two dot author  is equal to  tolkien  and booktube  pages is equal to let’s say 500. so  now i’m actually representing another  separate book inside of my program so i  can come down here and say like  puts  booktube.author  and this is going to print out the  author of book 2 over here in the  console  so essentially what we did again was we  defined the template for what a book is  going to be inside of our program  and this is a class  and then we can create instances of that  class which are called objects and an  object is just like an actual book so we  have the harry potter book we also have  the lord of the rings book down here  and we can access all of the attributes  from inside those books by saying  booktube.author or booktube.pages we can  just refer to them directly and that’s  one of the cool things about classes and  objects is they allow you to create your  own custom data types inside of ruby and  one of the cool things about ruby is  that everything all data is actually  classes and objects so all data is  actually an object a string is an object  an integer like a number is an object a  floating point number is an object a  boolean is an object all these different  things are objects and they all have  classes which define them that’s really  one of the cool things about ruby and  one thing that sets it apart from a lot  of other programming languages is that  everything is an object  in this tutorial i want to talk to you  guys about the initialize method in ruby  classes so in the last tutorial we  looked at creating ruby classes and then  creating objects of those classes in  this tutorial i’m going to show you a  way that we can actually make creating  these objects a little bit easier  so you’ll notice down here when i  created my two book objects i said book  one is equal to book dot new and then i  had to manually set the book’s title the  book’s author and the book’s pages so  just to create one book i had to type  out four full lines of code right down  here i’d do the same thing so when i  created the second book had to type out  four full lines of code and this is kind  of a problem because if i wanted to  create for example like you know 10 or  20 different books it’s going to take up  seriously a lot of space inside of my  ruby files also just having to go  through and manually say like book one  dot title is equal to harry potter book  one dot author like that gets really  tedious after a while there’s actually a  way that we can give our objects all  this information right up front when we  create them instead of having to do it  manually like this and that’s by using  something called an initialize method an  initialize method is actually a method  that’s going to get called whenever we  create an object so remember up here we  have this class and this class is  basically just a template it’s a  blueprint for what a book is in our  program when i create an object it stops  being a template it stops being a  blueprint and it becomes an actual book  so this book one object down here is  representing the harry potter book you  know with the author of jk rowling and  400 pages this is like a physical book  that we used that template up there to  create so when we end up creating a book  we can actually give it some default  information and i’m going to show you  guys how to do that so up here in our  class i want to create a method and it’s  going to be called initialize so i’m  just going to say def and it’s  initialized just like that and i’m  actually going to create this just like  i would a normal method so i’m going to  say n down here  inside of these parentheses  i can actually pass some information but  before i do that i want to show you guys  what’s going to happen so for example i  could put a print statement here  actually let’s do a put statement and  it’s going to say  creating  book  now when i run my program  you’ll see that over here  it actually types out creating book  twice  essentially what’s happening when i say  book dot new  this initialize method up here is  getting called so every time i use this  new method down here and i call it  this initialize method is actually  getting called so if i was to pass a  parameter into here so let’s have this  initialize method take a parameter like  we’ll have it take name i could come  over here and i could basically print  out like  hello  name  so i could pass a name into here i could  say like mike  and then down here i could say another  name like bill or whatever  and when i run my program you’ll see  it’s printing out hello mike and hello  bill  so this is essentially just a method  that we can call just like any other  method in any of our programs but this  method’s special because it gets called  when we create an object so what we can  do is we can actually pass this  initialize method some information and  we can use that information to give our  object specific information so  essentially what i can do over here is i  could say initialize is going to accept  three parameters so when we call this  new method down here we’re gonna have to  pass it three things the first thing  we’re gonna have to pass it is the title  of the book that we wanna create the  second thing we’re gonna have to pass it  is  the author of the book we want to create  and the third thing is going to be the  number of pages for the book that we  want to create so now whenever i want to  create a book i have to give it a title  an author and a number of pages once i’m  inside this initialize method i can do  something special so i can actually take  the values that the user passed in so i  can take the title the author and the  pages  and i can assign them to the attributes  of the object so i can assign them to  the title of the object the title of the  author and the title of the pages i can  basically do what i did down here so i  can basically say like book one dot  title is equal to whatever the title  they passed in was and this is going to  save us tons of time when we’re writing  our programs  and the way that i can do that as i can  i can just say at and i can say title is  equal to title so let me walk you guys  through this line of code when i say at  title  this is referring to the title attribute  in our object it’s referring to the  title attribute that’s we defined up  here it’s referring to that title up  there right this is saying the title of  the object that we’re creating  is going to be equal to the title that  the user passed in  remember this title this is just a  parameter that got passed into this new  method down here this is just a  parameter just like i passed in the name  it’s the same thing we’re passing it a  parameter  and i i could even name this something  else i could name this like you know i  couldn’t i could name it you know  whatever i wanted and but i’m just  calling it title because that’s what it  is it’s the title so i’m setting the  title of the object equal to the title  that got passed in i can do that for all  of these so i can say the author of the  object is going to be equal to the  author that got passed in  and  the number of pages is going to be equal  to the number of pages that got passed  in  and so essentially what i can do now is  when i create my new book instead of  having to say like  book1.titlebook1.author  i can just take all of this information  and put it up here so i can pass it into  this initialize method so i can just say  harry potter  and this is going to be jk  rowling and then the number of pages is  just going to be 400.  so i can actually get rid of all of this  code over here because i don’t need this  anymore and i can do the same thing for  this other book down here so we’ll pass  in lord of the rings  tolkien and 500 pages and i can actually  just get rid of all of this stuff  so now  instead of having all that other stuff  like where i had to like manually set  each of those attributes  i can reduce the lines of code i have to  write down to two and this is gonna do  exactly the same thing as we were doing  before so i could come down here and i  could you know print out like  book two dot title  and it’s still gonna be able to print it  out so you’ll see it’s printing out lord  of the rings i could print out book one  dot author  and it’s going to be able to print that  out so i’m doing exactly what i was  doing before except now i’m making it a  lot easier on myself by using this  initialize method and again whenever we  say like book dot new  this means we’re calling this initialize  method we’re passing it some parameters  and i’m setting the attributes of the  current object equal to the attribute  that we pass in when we create that  object so that’s how we can use that  initialize method to make our lives way  easier and you’re always going to want  to use an initialized method for the  most part just because it makes  everything so convenient  in this tutorial i want to talk to you  guys about instance methods in ruby now  sometimes you’ll hear people call these  instance methods you’ll also hear people  call them object methods sometimes  they’ll even call them class methods  essentially what we’re going to do is  we’re going to come inside of a class  that we create in ruby and we’re going  to give it some methods and then when  we’re using objects of that class we can  access those methods to get information  about our objects so i want to show you  guys my setup over here i actually have  a class here called student and this  class is basically just modeling or it’s  acting as a blueprint or a template for  a student in our program essentially we  created like a student data type so i  defined the attributes for a student to  be name major and gpa and down here we  have this initialize method so we’re  passing it a name a major and a gpa  and we’re setting the attributes of this  student object of an individual student  object to be equal to what gets passed  in so this is like our student class  and down here i’m creating two student  objects so we’re making one his name is  jim he’s studying business he has a gpa  of 2.6 then we have pam who’s studying  art and her gpa is 3.6 so these are  student one and student two now imagine  that for each of the students in our  program we wanted to be able to figure  out if they had honors or not so imagine  you’re writing a program for a college  or university and you wanted to be able  to figure out whether or not a  particular student had honors let’s say  that the rules for honors would were  constantly changing right so one day  maybe you had honors if you had a gpa  over 3.5 and maybe then they you know  would change it to be like 3.3 in other  words like imagine if we wanted to be  able to find out which students had  honors and which didn’t  we can actually write a method inside of  our student class  and then that method will be able to  tell us whether or not a specific object  has honors so i’m going to show you guys  how to do that over here in our class  i’m just going to come down here below  this initialize method and we can  actually create a method of our own so  i’m going to go ahead and define a  method and i’m just going to call this  has honors  and this method is not going to take in  any information so we’re not going to  need open and closed parentheses and  down here we’re just going to end it off  so  this has honors method is basically  going to return either true or false  if the current student if the object  that’s calling this method has honors  it’ll return true if they don’t it’s  going to return false  so how can we figure out if the student  has honors well we could use an if  statement so i could say if i want to  check to see if the student’s gpa is  greater than or equal to a specific  number so what i can do is i can  actually access the gpa attribute inside  of our student object and use it here so  i could say if  at gpa  is greater than or equal to and let’s  say that in order to have honors we we  have to have a 3.5 or above so if their  gpa is greater than or equal to 3.5  then we’re going to return true  otherwise  we’re just going to return false  so this is basically our method if the  gpa is greater than 3.5 we return true  otherwise we return false so now what i  can actually do is i can use this has  honors method on each one of my student  objects so i could come down here and  you see i’ve created these two students  right pam and jim  if i wanted i could print out whether or  not they have honors so i could say puts  and let’s check to see if student one  which is jim has honor so i could say  student one dot has  honors  and when i run this it’s going to print  out whether or not he has honor so in  our case it’s going to be false  but if i ran this method on student 2 so  if i ran this method on pam pam actually  does have honors because she has a 3.6  so now this is going to return true  and what i want to point out is this has  honors method is going to be different  depend or it’s going to be using  different information depending on what  object is calling it right so when  student one when jim is calling this  object jim has a gpa less than 3.5 so  it’s going to return false for jim so  for the gym object for the gym instance  it’s going to return false but for the  pam object pam’s gpa is higher than 3.5  so it’s going to return true we can  basically define a method that can be  used on all of the objects for our  specific class and we can do it using  something like this so you can define as  many of these as you want inside of your  class and these can be really useful a  lot of good classes out there in ruby  are gonna have a bunch of good methods  like this which are gonna allow you to  you know either find out information  about the specific object or you know  modify the object or do something to the  object in some way shape or form so  that can be really useful and just  consider you know writing some methods  inside of your classes when they’re  appropriate this is a good example of  you know where a method can come in  handy because it can tell us whether or  not the student has honors he can tell  us something about the student using the  attributes using the information that  we’ve stored about that object  in this tutorial i want to show you guys  how we can create a multiple choice quiz  in ruby so we’re going to use some of  the stuff that we’ve learned so far in  this course like classes we’re going to  use loops we’re going to use variables  and if statements in order to create a  multiple choice quiz basically we’re  going to be asking the user a series of  questions the user is going to input  whatever answers they think are correct  we’ll grade the test and we’ll tell the  user how they did so this is going to be  pretty fun first thing i want to do is  talk to you guys about some stuff that i  already have set up first thing i did  was i wrote out a few questions for our  multiple choice test down here i have p1  p2 and p3 and these are all basically  prompts so this is what we’re gonna  prompt the user with and we’re gonna ask  them the question so over here it says  what color are apples red purple or  orange what color are bananas pink red  or yellow what color are pairs yellow  green orange so this is a really easy  multiple choice test just asking about  the colors of different fruits  over here i’ve defined a class called  question  and this is basically modeling a  question in our program so i’ve  essentially created like a question data  type and down here we have these  attribute accessors so we’re defining a  question has a prompt and an answer so  the prompt is basically what we’re  asking the user so it would be like for  example all these questions down here  and then the answer is the actual answer  to the question so like we’re giving  them a prompt that has a bunch of  different options multiple choices the  answer is going to be like what the  actual answer is and then we have this  initialize method which will just  initialize the  object so we can create like questions  inside of our program now so what i want  to do is i want to create an array of  questions and we’re going to have one  question for each of the prompts that we  have up here so i’m actually just going  to create an array we’re going to call  it questions  and i’m just going to set it equal to  a bunch of different questions so  actually inside of this array we can  create a couple different questions so  i’m going to say  question.new and i’m going to create  this first question so remember we need  to give this a prompt and we need to  give it an answer so for the first  question i’m going to pass it p1 as the  prompt because this is the prompt for  the first question and i want to give it  the answer so the answer to what color  are apples is going to be a  red so i’m just going to pass in an a  now i can create another question so  you’ll notice over here i can just  create a question by saying question dot  new and then i can pass in the initial  information so i’m going to say  question.new and now we’ll make one for  p2 for that second prompt it says what  color are bananas and the answer is  going to be c  yellow so we’ll put a c inside of here  and finally we’ll make another question  and this is going to be p3  and the answer to what color are pairs  is going to be b green so we’re going to  go ahead and put a b in here essentially  what i’ve done is i’ve created three  questions in my program and i’ve stored  them inside of this array called  questions  so we have an array that stores all the  questions for our test now the next step  is we need to actually be able to run  the test so what i want to do is i want  to be able to go through each of the  questions in the test i want to be able  to ask the user the question i want to  be able to get their answer and i want  to figure out whether or not they got  the answer right so what we can do is we  can actually create a method so why  don’t we create a method that will run  the test so this method will accept one  parameter which is going to be an array  of questions it will use those questions  to run the test and it’ll basically like  ask the user all the questions figure  out what their answers were and score  and grade the test so let’s do that down  here i’m going to create a method and  we’re just going to call it run test and  this method is going to take one  parameter so it’s going to be  an array of questions so we could say  questions  and we’re just going to call the  parameter that we pass in questions  and we can go ahead and end off this  method now inside of this method we want  to do a few things the first thing we  want to do is basically just loop  through and ask the user all the  questions so what i want to do is create  a variable called answer and i’m just  going to set this equal to the empty  string initially  and what we’re going to do is we’re  going to store all of the user’s answers  inside of that answer variable so like  we’ll store you know the answer to the  first question the second question the  third question etc  now what we want to do is we want to  loop through all of the questions inside  of this questions array so i’m going to  create a loop i’m just going to say for  question in  questions so for each question inside of  this questions array i want to do  something and down here we’ll just end  this off now inside of here essentially  what i want to do is ask the user the  question so i’m basically going to say  puts and i’m going to print out the  prompt so i can say puts  question  dot  prompt so remember  this is going to be an array of question  objects and the question class has two  attributes it has a prompt and it has an  answer so essentially what i’m doing  down here is i’m printing out the prompt  for the current question that we’re  looping through  once i’ve printed out the prompt i can  get the input from the user so i can  just say answer is equal to gets.chomp  and this is basically just going to get  whatever the user enters and it’s going  to store it inside of answer now what we  need to do is we need to be able to keep  score so we need to be able to keep  track of how many questions the user  gets right and how many they get wrong  so i’m going to create another variable  up here called score and i’m going to  set this equal to 0 initially and  whenever a user gets a question right  we’re going to increment the score so  every time they answer the question  correctly score will get incremented and  by the end of the of the test we’ll be  able to tell how many questions they got  right so down here i want to check to  see if the answer that they gave is  equal to the correct answer so i can  just say if  answer  is equal to and now i want to compare  this to the answer to the question so i  can say question dot answer and remember  this is just another attribute from  inside of that questions class  so down here i’ll just end this off and  if the answer is equal to  question.answer then i can basically  just increment the score variable so i  can say score plus equals one and that  will increment the score now finally  down here after this for loop i’m just  going to print out how they did so i’ll  say puts and we’ll basically just say  like you got however questions out of  however right so will basically be like  puts  you got  score  out of  and now we’re just going to type out the  total number of questions so we can say  questions.length  and this is just going to tell us how  many questions were inside of that  questions array  so this is obviously a lot of code and  you know this run test method is  obviously a lot of code but  everything looks like it’s right so why  don’t we try to run this and we’ll see  how we do  so remember whenever we’re going to get  input from the user we always want to  use the command prompt or the terminal  to do that so i’m going to open up my  terminal  and i’m going to go ahead and run this  ruby file so i’m just going to say ruby  draft.rb because that’s the name of the  file  and we’ll run this and so actually  whoops totally forgot we have to call  this run test method so i didn’t  actually call this run test method so i  need to do that so let’s come down here  and we’re just going to call it so i’m  just going to say run test and we’re  just going to pass in that questions  array so we’re passing in the array of  questions that we created  up here we’re passing in this guy and  actually you know there’s one more thing  we have to change so down here i’m  printing out the score i have to say dot  to s  so we’re converting the score into a  string and we’re gonna have to do the  same down here so i’ll just say dot to s  and now that we have this we can  actually run our program from inside of  the terminal so let’s go ahead and do  that so here in the terminal i’m just  going to run this file i’m going to say  ruby  draft.rb  and you’ll see it’s asking us the first  question so it says what color are  apples so let’s just try to get them all  right i’m going to say apples are a red  what color bananas let’s say c yellow  what color are pairs let’s say b green  and so you can see here once we entered  in all the answers it typed out you got  three out of three correct so it’s  actually telling us how many of these we  got correct and that’s pretty awesome so  let’s try this again but let’s try to  get some wrong so i’m going to run this  same exact file  and let’s get the first one wrong we’ll  say apples are purple we’ll get the  second one wrong we’ll say bananas are  pink and we’ll get the third one right  so we’ll say pairs are green so now it’s  gonna say you got one out of three  because we only got one out of three  questions correct so that’s basically  how we can build a little multiple  choice test  in this tutorial i want to talk to you  guys about inheritance in ruby an  inheritance basically allows us to  extend the functionality of one class  into other classes so i can basically  define what we would call like a super  class and then i can create sub classes  from that super class which will inherit  all of the methods all the functionality  all of the attributes from that original  super class so if that doesn’t make  sense don’t worry i’m going to give you  guys a full example of what this is and  how it works  but let me show you guys first what i  have over here so i created a class over  here which is called chef  and this chef class i’m going to expand  it it has three methods inside of it so  the first method over here is called  make chicken and this method basically  just prints out the chef makes chicken  and then it has a make salad method same  thing prints out the chef makes salad  and then finally we have the make  special dish method which just prints  out the chef makes barbecue ribs so  essentially we’re just like creating a  chef inside of our program if i wanted i  could come down here and create an  instance of the chef so i could say  chef is equal to chef dot new  and now that i have my chef object i  could say like chef dot make  chicken  and now when i run my program you’ll see  it says over here the chef makes chicken  so we’re telling the chef to make a  chicken we can tell the chef to make a  salad or to make the special dish  now  let’s say that in addition to having  this chef class right this general  overall chef class we also wanted to  define another type of chef we also  wanted to model another type of chef in  our program so let’s say that in  addition to this generic chef we wanted  to create a  more specialized type of chef so we  wanted to create an italian chef so i  could say  italian chefs we’re creating an italian  chef class and down here we’ll just end  this off  but let’s say that our italian chef  can do everything that our normal chef  can do so the italian chef can also make  chicken can also make a salad and can  also make a special dish so the italian  chef can do  everything that the normal chef can do  in other words the italian chef can have  all the same methods as the normal chef  well i can actually use something called  inheritance and i can pass down all of  the functionality from inside of this  chef class into the italian chef class  in order to do that all i have to do is  come down here and after i say the name  of the class i can just make a less than  sign and i can just type out the name of  the class that i want to inherit from so  when i say chef right here  that means that the intel the italian  chef is going to inherit all of the  functionality from this chef class so  it’s essentially going to inherit all of  these methods  so let me demonstrate this you’ll notice  there’s nothing inside of this class i  didn’t type anything inside of there but  i could come down here and i can create  an italian chef object so i could just  say  italian chef  chef dot new so i’m creating a new  italian chef and then down here i can  just say italianchef.make  salad  so  even though i don’t have any code up  here inside of this italian chef class  not a single line of code  i can create an italian chef object and  i can still tell this italian chef to  make salad so now when i run my program  you see down here in addition to the  normal chef making chicken we’re also  able to use the italian chef to make a  salad so over here it says the chef  makes salad so this italian chef object  has access to all of the functionality  from inside of the chef class because i  inherited it so i’m using inheritance  here and i’m inheriting all the  functionality from the chef class but  here’s a question right  the normal chef over here has a special  dish so the normal chef makes a special  dish and it says the chef makes barbecue  ribs but let’s say that the italian chef  is going to have a different special  dish from the just generic chef what i  can do is i can actually do what’s  called overriding a method so i can  override the make special dish method  inside of this italian chef class so i  could actually say  make  special dish  and we’ll end it off and then inside of  here i can put what i want the italian  chef’s special dish to be so i could  just say puts and we’ll say like the  chef makes  eggplant parm so this is going to be the  italian chef’s special dish so now if i  was to come down here and say chef.make  special dish  and italianchef.makespecialdish  you’ll see when i print these out or  when i run my program it says the chef  makes barbecue ribs and the chef makes  eggplant parm so inside of the italian  chef class i was actually able to  override this make special dish method  and that is a super useful thing to do  another thing i can do is add  functionality into this so let’s say  that the italian chef in addition to  doing everything that the normal chef  can do can also make pasta so i can make  a method here it’s make pasta  and down here it’s just gonna be  the chef makes pasta  so now inside of my italian chef object  i can  make some pasta so i could say  italianchef.make  pasta and the italian chef will be able  to make pasta but the normal chef down  here doesn’t have a make pasta method so  it’s not going to be able to make pasta  so let’s go over what we did essentially  i created this class chef this chef had  a bunch of functionality it could make  chicken can make salad can make a  special dish right  then i created another class italian  chef and this italian chef could do all  the same stuff as the normal chef  so what i did was i inherited all the  functionality from the chef class into  here right but there was a circumstance  where the italian chef was actually  going to have a different special dish  than the normal chef so i overrode this  method i basically defined redefined the  method inside of the italian chef class  and i was able to make it do what i  wanted it to do i also extended the  functionality so i was able to actually  make the italian chef different so the  italian chef in addition to doing  everything that the normal chef could do  could also make pasta and that’s the  basics of inheritance we can define  what’s called a superclass so this chef  up here this is the superclass and then  we can also define subclasses so this  italian chef is a subclass and it’s  considered a subclass because it  inherits from the chef superclass and  this can be really awesome so a lot of  times in ruby you’re going to have  different hierarchies of classes so  we’ll have like this chef class and then  we’ll have a subclass an italian chef i  could also create other subclasses like  a chinese chef or a french chef or a  mexican chef right i could have  different you know  types of chefs different subclasses of  chefs that would all inherit the  functionality from the generic chef  class so that is super useful feature in  ruby it’s really going to come in handy  especially if you start creating lots  and lots of classes  in this tutorial i want to talk to you  about using modules in ruby now module  is basically just a container where we  can store groups of methods so as you  start programming more and more in ruby  and as your programs get more and more  complicated you’re going to want to  organize all the different methods that  you’re using so a lot of times in a ruby  script you’re going to be writing out a  bunch of different methods that are  going to do different things and they’ll  have different tasks  and one thing we can do is we can take  methods that are doing similar things or  that should be grouped together we can  put them inside something called a  module  so over here i have this file open it’s  called useful tools.rb  and in here i’ve defined a module so i’m  just going to open this up and you’ll  see inside of this module i just said  module and then tools so whenever we  create a module you can create it by  saying module and generally you’re going  to want to give modules a  name with a capital letter so you need  to do that  and down here i have two methods inside  of this module i have a method called  say hi and it says hi to a user and i  have one called say bye same thing give  it a name and it says goodbye this is a  very very simple module but  the module is essentially just storing  these methods inside of it  and this basically acts as a container  for us to organize our methods and sort  of keep them in a nice clean little  container  so if i wanted to use the methods that  were inside this module the first thing  i have to do is just come down here and  say include  and now i just want to type the name of  the module so i can say tools  now if i wanted to access the methods  inside of the tools module i could just  say tools dot and now i can type out the  method name so i could type out dot say  hi  we could pass up my name  and now it’s going to say hi to me so  it’ll say hello mike you do the same  thing for say bye  so i’m able to store these different  methods inside of this tools module and  then i can access them just by referring  to the name tools and then the method  name so modules are useful for really  two reasons the first reason is because  they allow us to organize our methods a  lot better so if you have a bunch of  methods you can put them inside of a  module and then they’re just sort of  like organized in their own separate  container  the other thing that makes these useful  is we can use this name so i can give a  module the name  and then i can basically say like over  here i want to call the say by method  inside of the tools module or i want to  call the say hi method inside of the  tools module it basically gives all of  these methods like it gives them a  namescape so this is the say by method  inside of the tools module basically  allows you to you know refer to these  different methods inside of a particular  namescape and that can be really useful  so for example i could have a say hi  method inside of the tools module and i  could have a say hi method inside of  like another module and i’d be able to  use both of them in the same program so  modules are extremely useful and this is  how you can use a module in the same  file so i declared this module up here  and i was able to use it in the same  ruby file  but a lot of times you’re going to want  to use these modules and other files so  for example i have this draft.ruby file  over here imagine that i wanted to use  all the functionality from the tools  module  inside of this file i’m going to show  you guys how to do that the first thing  we need to do is we need to actually  like require this file so there’s a  keyword in ruby it’s called  require and we can type out require and  then we can type in the name of a file  and we’ll be able to basically like grab  all the information and bring it into  our file and actually there’s a command  called require relative and this is what  i’m going to use require relative will  basically allow you to input a file name  relative to the current file so you  notice over here in my file tree i have  this draft.ruby file open and ruby tools  are these this useful tools files in the  same directory so if i use require  relative i can just type out the name of  that file so i can just say  useful tools.rb basically this is just  like  telling ruby that we’re going to use  something that’s inside of this file so  we need to require it and now what i  want to do is i want to  include  tools so i’m going to do the same thing  we did over there in that other file  but just inside of here so if we run  this draft.rb file now essentially we’re  going to have access to this tools  module so i could come down here and i  could say tools dot  say hi  and we can say like mic and now this is  going to run that tools module but  we didn’t actually define the module  inside of this file we defined it over  here so this is something that’s really  useful  in ruby you can basically define a bunch  of these different modules and then you  can include them into different files  and you’ll be able to use all the  functionality inside of those modules  in this tutorial i want to talk to you  guys about interactive ruby now  interactive ruby is basically a little  application that we can run from the  terminal or the command prompt and it  essentially allows us to test out and  use different ruby commands in a sort of  sandbox environment so i’m going to show  you guys exactly what it is and how to  use it the first thing you want to do is  open up your command prompt or your  terminal so if you’re on windows open up  your command prompt if you’re on mac  open up the terminal  and i’m just going to run this and i’m  just going to type in terminal  so now that i have my terminal open in  here i want to make sure that i have irb  installed so sorry irb stands for  interactive ruby and this is the program  that we’re going to be running so all  you want to do is type irb  hyphen v  and this should spit out a version  number now as long as you have ruby  installed on your computer you’re  probably going to have irb  but if you don’t have irb installed so  if you didn’t get a version number for  that and just go online and you can look  up you know how to install irb and  here’s the thing it’s not absolutely  necessary for ruby so maybe just like  watch this tutorial see if it’s  something that you think you’re going to  use and then go off and download it but  if you have it you should be able to  follow along with me so i’m just gonna  type irb i’m gonna hit enter and now  you’ll see that i get this little prompt  with this uh sort of like greater than  sign and essentially this is interactive  ruby so this is the irb it’s essentially  like a little application that we can  use and we can just test out little ruby  commands so for example i could create a  variable let’s call it num i could set  it equal to  123.  i’m just going to click enter and now i  can test out a ruby command so i could  say like puts  one two three and this is gonna go ahead  and print out the number that was stored  in the variable so essentially you can  execute any like valid ruby commands  from inside of the irb so i could create  like a method and we’ll just call it say  hi  and down here you’ll see now i can write  whatever is going to be inside the  method so i could say puts  hello  and now we can end off the method so i’m  just going to click end  and now i can actually like use that  method so i can do like say hi and  this is going to say hello so you can  find methods you can find variables you  can define all different types of stuff  you could use things like if statements  or you could use you know while loops  you can use all the valid ruby commands  that you can normally use inside of a  file inside of this interactive ruby  but basically this is just an  environment where you can go to test out  you know different functionality you  could test out a little method you can  test out a function and it’s an  environment where there’s literally no  setup required so you don’t have to like  create a text file you don’t have to run  the text file from the terminal you can  just go in here and try different things  out and it’s like very much like a  sandbox environment now here’s the thing  this is really good if you’re just  trying to like test certain things out  or you know see if something’s going to  work  but generally if you’re going to be  writing like actual ruby scripts or  actually ruby actual ruby commands you  want to do that inside of a file so this  isn’t a replacement for  like writing code out in a file and like  doing all that this is a special  environment that you want to use  basically for like testing purposes  but this can be really useful and a lot  of times if you go online and you see  like different ruby tutorials they’ll be  like using the interactive ruby command  line just because it’s like so useful  and so easy to set up so really i just  wanted to kind of introduce you guys to  this and expose you to it so you know  what it is and like what it’s doing but  i would say for the most part you know  you should be writing your ruby code  inside of a text editor  

admin

Leave a Reply

Your email address will not be published. Required fields are marked *