00:00:00well hello internet and welcome to my
00:00:01Ruby programming tutorial in this one
00:00:03tutorial I’m going to cover pretty much
00:00:0595% of the Ruby programming language all
00:00:08of the basic syntax and Ruby is
00:00:10basically a dynamic interpreted
00:00:12object-oriented language in which
00:00:13everything is an object and all of those
00:00:15objects can be overwritten Ruby is one
00:00:18of the easiest languages to read and
00:00:20learn and with rails it just might be
00:00:22the best web development option period
00:00:24in the description underneath this video
00:00:25you will find easy links to all the
00:00:28individual pieces of the Ruby
00:00:29programming language that I cover as
00:00:31well as a link to all of the code that
00:00:34is heavily commented and easily
00:00:36transcribed well enough talking let’s
00:00:38start writing some code okay on the left
00:00:40side of the screen I have a basic text
00:00:42editor which is called sublime text I’m
00:00:44trying a different color option here so
00:00:46if you guys liked it please tell me in
00:00:47the comments section below and on the
00:00:49right side of the screen I have the
00:00:50terminal or command line if you were on
00:00:52Windows and if you want to install Ruby
00:00:55I’m not going to go through that it’s
00:00:56very easy just go to Ruby – lang org and
00:00:59then whatever and installation and here
00:01:02you’ll find all the information you need
00:01:04as well as easy installers depending
00:01:06upon what operating system you’re using
00:01:07all right so in this first part I’m just
00:01:10going to briefly cover a whole bunch of
00:01:12different things
00:01:12Wow if you want to put a comment in just
00:01:14put a hash symbol and that is a comment
00:01:17and I go over other different comments
00:01:19as well as we continue you want to print
00:01:21something to the screen very easy to do
00:01:23you could say something like enter a
00:01:24value like that and then variables are
00:01:27either going to start with lowercase
00:01:29letters or an underscore normally
00:01:32lowercase letters and of course after
00:01:33the underscore or lowercase letters
00:01:35you’re then going to be allowed to use
00:01:37numbers in your variable names let’s say
00:01:39I just go and create the normal way of
00:01:41creating variables is to use dashes
00:01:43between or underscores between all the
00:01:46different words in your variable names
00:01:47and if I want to get data from the user
00:01:50I just type in gets and then if I want
00:01:52to convert that from a string to an
00:01:54integer I would just put dot t– o–
00:01:56underscore i like then ask them for
00:01:59another value enter another value and
00:02:02then get that one as well using gits
00:02:05once again and it’s very important to
00:02:07convert
00:02:07to an integer then another way we can
00:02:09output data is using puts and the
00:02:12difference between print and puts is
00:02:13print will not put a new line after it
00:02:15prints out your string that on the
00:02:17screen while puts will puts a new line
00:02:20and let’s say that we wanted to say
00:02:22something like first number we want to
00:02:24convert this back to a string to do so
00:02:26just put two underscore s if you want to
00:02:29concatenate or combine strings you just
00:02:31put the plus sign and I could put
00:02:32something like plus like this and then
00:02:34continue on with second number two
00:02:37underscore s plus equal two and you can
00:02:40see I’m just continuing to add these
00:02:41together you can skip over to the next
00:02:43line and of course you do not need to
00:02:45put a semicolon at the end of every
00:02:46statement and then if I wanted to
00:02:48actually perform some arithmetic inside
00:02:50of here I can do so and then I would
00:02:51just convert that to a string and if we
00:02:53come over here and you can see the name
00:02:55of my file is Ruby or not Ruby and I’m
00:02:58just going to type in Ruby to execute it
00:03:00followed by the RB extension which is
00:03:03the extension for all Ruby files if I
00:03:05execute it you can see enter a value I
00:03:06can type in two and another value three
00:03:08and there you can see the answer pops up
00:03:10on the screen now there are five main
00:03:12rithmetic operators and we of course
00:03:14have addition subtraction multiplication
00:03:16division and modulus which returns the
00:03:19remainder of a division see over here if
00:03:21I enter those in you can see the answers
00:03:23that show up there on the screen now
00:03:24integers or fixed numbers can store
00:03:27extremely large numbers that do not have
00:03:29decimal places and you can pretty much
00:03:30trust an integer can hold well beyond
00:03:33the number that I displayed here on the
00:03:34screen so they are quite large and Ruby
00:03:36uses a different way of storing integers
00:03:39that allows you to basically be able to
00:03:41store numbers that are that large now
00:03:42let’s say I come in here and let’s
00:03:44create another integer just to show you
00:03:46an example of the accuracy of floats and
00:03:50this is a float let’s just say it’s one
00:03:52point zero zero zero and then let’s
00:03:54store another float inside of here and
00:03:56one thing to remember about floats is
00:03:58there must always be a number before the
00:04:00decimal place whenever you define them
00:04:02and then we could come in here and just
00:04:04go num1 and convert that to a string
00:04:07plus here let’s do some subtraction this
00:04:10time no I’m 99 again convert it into a
00:04:12string plus I’m 1 minus num 99 string
00:04:17say
00:04:18if we execute it you can see that it is
00:04:19quite accurate up until it gets to the
00:04:21very very end of this float where it has
00:04:24that nine basically with floats you can
00:04:26trust that 14 digital accuracy is a
00:04:28basic norm so let’s come in here and
00:04:31create a big float and there you go and
00:04:33then let’s perform a little bit of
00:04:34arithmetic here big float plus remember
00:04:38always put the zero there and you can
00:04:40see that while it will be able to store
00:04:42up to fourteen digits of accuracy if we
00:04:44try to perform a calculation like this
00:04:46that it is not so accurate in performing
00:04:48that calculation so just like every
00:04:50other programming language float
00:04:51arithmetic is a little bit off so you
00:04:54should be aware of that
00:04:55now I said before that every single
00:04:57thing inside of Ruby is an object and
00:05:00let’s just prove that if we come in here
00:05:01and type in one dot class which is going
00:05:04to return the class of whatever precedes
00:05:06it we could even put a float inside here
00:05:08dot class and of course we could come in
00:05:10and put a string inside of here dot
00:05:12class and what if we execute that you’re
00:05:15going to see that this comes back to fix
00:05:17number float and string will get more
00:05:19examples of exactly what a object is
00:05:22inside of Ruby basically everything now
00:05:24a constants going to start with an
00:05:25uppercase letter so we can put something
00:05:27like a constant like that and 3.14
00:05:30however you have to be aware that while
00:05:32we will say this is a constant that
00:05:34doesn’t mean we can’t come in and also
00:05:36change the value what’s going to happen
00:05:38though whenever we run this and we try
00:05:40to change the value for the constant
00:05:41we’re going to get an error but it still
00:05:43will allow us to change it anyway as you
00:05:45can see right there there is your
00:05:47warning da-da-da-da-da
00:05:48however the constant value did indeed
00:05:50change and then just to show you one
00:05:52more advanced thing let’s say that we
00:05:53want to return a file object it’s quite
00:05:56easy to do go write handler and we would
00:05:59use the file object I’ll do a more
00:06:01advanced tutorial on this later on at
00:06:03the end of this tutorial but let’s say
00:06:05we have a file called your sum we want
00:06:07to write two and we’ll just give this
00:06:09the out extension and then say that we
00:06:11want to write to it that’s going to
00:06:12create the file by the way if it doesn’t
00:06:14already exist we could then very easily
00:06:16come in and put some text into that file
00:06:19let’s say we wanted to put random text
00:06:22and you could put variables you can put
00:06:23pretty much anything you want inside of
00:06:24there but you do need to convert it into
00:06:26a string we would then
00:06:28in here and close the file after we are
00:06:30done with it and then if we wanted to
00:06:32read data from the file data from file
00:06:35its equal to we would just use the file
00:06:37object again and say read and then say
00:06:40what you wanted to read from say it’s
00:06:42called your some thought out and then
00:06:44put the data on the screen we could do
00:06:46that just that easily and as you can see
00:06:48data from file is random text just that
00:06:51easily we’re creating a file wrote to it
00:06:52closed it reopened it read from it now
00:06:54putted the data on the screen and then
00:06:56finally if you wanted to also come in
00:06:58here and load an execute code another
00:07:00Ruby file let’s say we have one called
00:07:02Ruby or not
00:07:03Ruby dot RB and let’s say give that to
00:07:07as a value we could then come over here
00:07:10and create Ruby or not Ruby – and just
00:07:13say hello from Ruby or not Ruby – save
00:07:17that and then jump back over here and
00:07:20there that is file save and execute and
00:07:22you can see that that is how easy it is
00:07:24to execute code from other different
00:07:25files now let’s get into performing some
00:07:28conditional operations another thing to
00:07:29remember though first off is if you want
00:07:31to have a multi-line comment all you
00:07:33would do is equal begin multi-line
00:07:35comments and then equals end that is a
00:07:38weird way of doing a multi-line comment
00:07:40but that is how they are created in Ruby
00:07:42now let’s get into conditional
00:07:43operations let’s say we have a variable
00:07:46called age and let’s give it the value
00:07:48of 12 we could come in here now and use
00:07:50if statements to compare values so we
00:07:52could say something like if age is
00:07:54greater than or equal to five we can
00:07:57also use logical operators like and or
00:08:00or not and then we go age is less than
00:08:03or equal to six we could then come in
00:08:05here don’t need the curly braces you
00:08:08might be used to say something like
00:08:09you’re in kindergarten like that we
00:08:13could then come in and go else if age is
00:08:15greater than or equal to seven and age
00:08:19is less than or equal to thirteen and
00:08:22I’ll put some different data you are in
00:08:26middle school something else that’s
00:08:28important is we’re going to be able to
00:08:30put multiples different statements in
00:08:31here so we could say yay and then we
00:08:33could say something like else puts stay
00:08:36home it’s very important to end your if
00:08:38statement we
00:08:39the term end or the key word end if we
00:08:42execute that you’re in middle school
00:08:43yeah shows up here on our screen now
00:08:45it’s very important to know there are a
00:08:47couple different comparison operators so
00:08:49the kids Paras and operators are going
00:08:52to be equal to not equal to less than
00:08:54greater than less than or equal to as
00:08:57well as greater than or equal to and
00:08:59then your logical operators which you’re
00:09:01going to be able to use to combine and
00:09:04differentiate those different
00:09:06comparisons is going to include and or
00:09:09and not and you could also type and or
00:09:12or not inside of there if you decided
00:09:14that would be something you’d rather do
00:09:16and just to give you a demonstration of
00:09:18those let’s say we have true and false
00:09:21is equal to plus true and false to
00:09:26string and let’s do the same thing for
00:09:28your other logical operators so here is
00:09:31or and or and then not let’s just say
00:09:35not like that and not like that which is
00:09:37going to return the opposite and there
00:09:39you can see exactly how those logical
00:09:41operators are work another comparison
00:09:42operator you have available to you say
00:09:45we’d go puts and we wanted to do a
00:09:48comparison it’s going to provide
00:09:50different answers depending upon how the
00:09:52logic works out for that comparison we
00:09:55have this little guy right here and
00:09:56basically what it’s going to do is it’s
00:09:58going to return 0 if both of those
00:10:00values are equal 1 if the first is
00:10:02greater and negative 1 if the first is
00:10:05less than the other one if we want to
00:10:07perform this operation just to see how
00:10:08this works out then to string and there
00:10:11you can see it shot back negative 1 and
00:10:13the reason why is the first is less than
00:10:14the other one just going to give you a
00:10:16rundown of all the different ways to
00:10:17compare just for completion reasons
00:10:19unless is another comparison operator
00:10:21and it just shows unless age is greater
00:10:25than 4 puts you could say no school else
00:10:29let’s go to school and then once again
00:10:33you would put end and you can see go to
00:10:34school comes up here and the reason why
00:10:36is this is only going to execute this
00:10:39line right here if age is less than 4 so
00:10:42that’s how in less works there’s a bunch
00:10:43of different ways to do comparisons
00:10:45inside of Ruby you’re also going to be
00:10:47able to come in here
00:10:48and add conditions to the output so we
00:10:51could say you are young is going to
00:10:54execute if age is less than 30 and you
00:10:58can see that it did another way of
00:10:59comparing things is using the case
00:11:01statement so we can say something like
00:11:03print enter greeting go and get that
00:11:05greeting from the user equal to gets and
00:11:08then we could put Chomp on the end of
00:11:10here if we wanted to remove the newline
00:11:12that goes with the input because the
00:11:14person types the input and it hits
00:11:15newline or enter and that’s what Chomp
00:11:18does gets rid of that newline we could
00:11:19then create a case statement that says
00:11:21greeting and then say when what they
00:11:24type in is equal to French we could also
00:11:26come in here and put multiple different
00:11:28stipulations so they can use lowercase
00:11:30or uppercase well then we’re going to
00:11:33say Bonjour
00:11:34once again when they type in Spanish or
00:11:37lowercase Spanish you could say hola
00:11:40like that and make sure that is exit and
00:11:42not end exit there you go and then
00:11:45finally we could type in else and this
00:11:48would substitute as the default and
00:11:50we’ll just put English inside of here
00:11:52and then we could put puts hello and
00:11:54then the end is actually the ending for
00:11:57the case execute and we could just say
00:11:59nothing and it’s going to say hello and
00:12:01we could have come in here and say
00:12:03Spanish it’s going to go jolla and we
00:12:05could also come in here and get the
00:12:07English and remove that all together and
00:12:09you’re going to get exactly the same
00:12:10results Spanish there you go another
00:12:12conditional we can use is what is called
00:12:14the ternary operator and basically it is
00:12:16going to provide different input
00:12:18depending upon and conditions so we
00:12:19could say if the age is greater than or
00:12:23equal to 50 for example and then we put
00:12:26a question mark it is going to return
00:12:28old as the output or the assignment if
00:12:30the condition is true otherwise it’s
00:12:33going to return young if the condition
00:12:35is false and there you can see young
00:12:37shows up let’s get into looping this
00:12:39first loop we’re going to demonstrate
00:12:41we’re going to create a variable give it
00:12:43a value of 1 and then we’re just going
00:12:45to type in loop do we could then come in
00:12:47here and go
00:12:48x+ or equal to one which is going to be
00:12:51the shortcut for X is equal to X plus
00:12:53one same exact thing both of these guys
00:12:56right here and you can also put comments
00:12:58right there in the same statement of
00:13:00course and let’s say that we wanted to
00:13:02come in here and only print out on the
00:13:04screen even numbers we could then type
00:13:06in next unless X modulus 2 is equal to 0
00:13:12and next what it’s going to do is jump
00:13:14back here to the top of the loop and
00:13:15continue iterating through the loop
00:13:17otherwise if we do have an even number
00:13:19let’s print that out on the screen and
00:13:21then let’s also use break which is going
00:13:24to throw us completely out of the loop
00:13:25if X is greater than or equal to 10 and
00:13:29that’s going to stop our looping and
00:13:30with the loop you’re going to also type
00:13:32in end you’re going to do that a lot of
00:13:33times if we execute that you’re going to
00:13:35see it prints out 2 4 6 8 and 10 so next
00:13:38if it gets down here it’s going to add a
00:13:40value of 1 to this which is going to be
00:13:422 and in that situation since 2 is an
00:13:45even number it’s going to print it out
00:13:47on the screen and then it’s going to
00:13:48continue it’s going to check is it
00:13:49greater than or equal to 10 no it’s
00:13:51going to jump back to the top of the
00:13:52loop it’s again going to add 1 this time
00:13:54we’re going to have 3 it’s going to say
00:13:56does this equal 0
00:13:57well the modulus of 3 + 2 is going to
00:13:59kick back 1 so it’s going to say that
00:14:01that indeed is true so it’s going to
00:14:03execute next which is going to jump back
00:14:04up to the loop again it’s going to add 1
00:14:06to it and you’re going to get a 4 and
00:14:08continue onward and onwards and then
00:14:10break of course like I said before
00:14:11whenever this condition is met and comes
00:14:13back with a true it’s going to continue
00:14:15execution after the loop and end the
00:14:18loop all together let’s go in here and
00:14:19create ourselves another variable to
00:14:21show you exactly how it while loops work
00:14:23inside of Ruby let’s give this value of
00:14:251 we could then come in and go while Y
00:14:28is less than or equal to 10 and then we
00:14:31could increment the value of Y by 1 we
00:14:34could then do exactly the same thing
00:14:36we’ll say next unless Y modulus 2 is
00:14:40equal to 0 print out on our screen again
00:14:43the value of y if it’s an even number
00:14:45and we could also put break inside of
00:14:47here and say if Y is greater than or
00:14:50equal to 10 we can kick ourselves out
00:14:52but the wild loops going to kick us out
00:14:54anyway because that’s already there so
00:14:55we can get rid of this all together and
00:14:57of course we’re going to
00:14:58while loop with end as well if we
00:14:59execute that you’re going to see 2 4 6 8
00:15:01and 10 print I on the screen again told
00:15:03you there was a bunch of different ways
00:15:04to loop and use conditionals inside of
00:15:06here let’s come up and let’s create
00:15:08another variable and this time we’re
00:15:10going to cover the until loop which is
00:15:12just a different way of doing things
00:15:13basically it’s going to do stuff until a
00:15:15condition is met
00:15:16versus end when a condition is met like
00:15:19the while loop works and to use it you
00:15:21just go until a is greater than or equal
00:15:23to 10 and then we’re going to increment
00:15:25the value of a is equal to 1 and we’re
00:15:28going to come in here then we could also
00:15:30put next inside of here if we’d like so
00:15:32that we can print our awed and even
00:15:34numbers to is equal to 0 and then in
00:15:37that situation we could print out on the
00:15:39screen
00:15:39the value and again until ends whenever
00:15:42end is typed in or reached in execution
00:15:44there you can see is another way to
00:15:46print out even numbers between 2 and 10
00:15:48it’s actually a couple different ways to
00:15:50use for loops all these are sort of
00:15:52considered for loops we could create
00:15:54here and array and I’ll get more into
00:15:56arrays later on it’s just basically a
00:15:58box that contains a bunch of different
00:16:00values in memory so there’s numbers we
00:16:02can come in and go for number in numbers
00:16:05and in this situation it’s going to be
00:16:07cycling through this numbers array and
00:16:09each one of them as we cycle through is
00:16:11temporarily going to be stored in a
00:16:13variable called number look at that now
00:16:15put them by going puts and if you want
00:16:17to have the value of a variable show up
00:16:20inside of a string you put the hash
00:16:22symbol right like that and we go number
00:16:24like this we could also throw a comm
00:16:26inside of there in a space if we’d like
00:16:28and this also ends with end and there
00:16:31you can see it cycles through all those
00:16:32and puts commas there on the screen
00:16:33we could also come in here and go print
00:16:35like this and that time you can see that
00:16:37there is no newline we could also come
00:16:39in here and define an array called
00:16:41groceries just to show that you can
00:16:43cycle through pretty much anything let’s
00:16:45say we have bananas sweet potatoes pasta
00:16:48and tomatoes there you go that’s our
00:16:51groceries array we could cycle through
00:16:53all of these by going groceries each do
00:16:56you put or food food in this situation
00:16:59is going to be the temporary holding
00:17:00cell for each item inside of our
00:17:02groceries array has its cycles through
00:17:04these there you go puts get some and
00:17:07then if we want to
00:17:08and put the value for food inside of
00:17:11there there we go
00:17:11and of course end this with end as well
00:17:14a whole bunch of different ways that
00:17:15iterating get some bananas sweet
00:17:17potatoes pasta and tomatoes I don’t
00:17:18prints out we could also cycle through
00:17:20numbers and this is very simple let’s
00:17:23just say we want to cycle through the
00:17:24number 0 through 5 this is called a
00:17:27range which we’ll cover more later on
00:17:29and then we can go each do again or is
00:17:32going to be the temporary value as we’re
00:17:34cycling through then we go puts hash
00:17:37symbol and then we could put I inside of
00:17:39here and let’s put another hash symbol
00:17:41here but this is going to actually be a
00:17:43hash and there that is and again that’s
00:17:45going to end with and execute and there
00:17:48you can see all those printed out so
00:17:49there’s pretty much every way we can
00:17:51both use conditionals as well as looping
00:17:53now let’s take a look at functions now
00:17:55functions are going to start off with de
00:17:57F and then you’re going to have your
00:17:58function name so it’s a add nums then
00:18:01you’re going to have parameters if any
00:18:03inside of here so we’ll say num1 and
00:18:06num2 and then you can either return a
00:18:08value or not return a value so I’m going
00:18:11to say num1 and i’m going to convert
00:18:13this into an integer and then num2 and
00:18:17convert that into an integer and then
00:18:19it’s going to return that value and
00:18:21functions also end with end we could
00:18:24then come in and go puts add nums and
00:18:27pass three and four into there and
00:18:29execute that you can see seven shows up
00:18:31on the screen now it’s very important to
00:18:33understand when you’re dealing with
00:18:34functions the variables are passed by
00:18:36value so their value can’t be changed
00:18:38inside of a function so that’s true even
00:18:41if you have the same name so let’s have
00:18:42X is equal to one and then we’ll create
00:18:44a function and change that and we will
00:18:47pass supposedly X inside of it then
00:18:50we’ll go and give X the value of four
00:18:52inside of the function we will then call
00:18:55change X and we will pass X but this is
00:18:59this X here we’re passing doesn’t have
00:19:01anything to do with that and you’re
00:19:02going to see here if we go puts X is
00:19:05equal to dot and then we’ll put the hash
00:19:07some one side of here and X
00:19:09and execute it it’s going to print out
00:19:12one so any value that changes inside of
00:19:15a function is not going to affect an
00:19:16outside value and this is very important
00:19:19to remember whenever you’re working with
00:19:20functions
00:19:21I’ll get more in functions later on but
00:19:23that’s the basic gist of how they work
00:19:24so now let’s take a look at exceptions
00:19:27now exceptions inside of Ruby a go by
00:19:29different names we’re going to be able
00:19:31to catch exceptions in Ruby using both
00:19:33begin and rescue so we’re going to say
00:19:36print enter a number and once again
00:19:40we’re going to get that value and store
00:19:42it in a variable called first num and
00:19:44we’ll use gifts and then convert it from
00:19:47a string into an integer and we’ll then
00:19:49say that we want to enter another number
00:19:52and we’ll store this in a variable
00:19:54called second num and we’ll use gets
00:19:56again now if we think we might have an
00:19:59error we’re going to put again and then
00:20:01we’re going to put some code inside of
00:20:02here that might throw an error so we’re
00:20:04going to say answer is equal to first
00:20:06num / 2nd num then after that we’re
00:20:09going to type in rescue which is going
00:20:11to be called if there’s an error and
00:20:13here we’re expecting the error to be a
00:20:15division by zero so we’re going to say
00:20:17you can’t divide by zero and then we’ll
00:20:21put an end which is actually going to
00:20:22end the begin and also we could come in
00:20:25here and throw exit now if we would come
00:20:27in and go puts first num / 2nd num is
00:20:32equal to and put answer inside of here
00:20:34and make sure you put a closing brace
00:20:36right there as well and execute it let’s
00:20:38split three inside here and then put
00:20:40zero and you’re going to see you can’t
00:20:42divide by zero shows up however if we
00:20:43would go and do something logical like 4
00:20:45+ 2 + 3 whatever it doesn’t matter you
00:20:48can see that it does indeed work you can
00:20:50also throw your own exceptions with rays
00:20:52so let’s just come in here and delete
00:20:54all this and create a function called
00:20:56check age and pass age inside of here
00:20:59and give a value of equal to 12 we could
00:21:02then say raise argument error and we
00:21:06could say enter a positive number is
00:21:09going to show up unless age is greater
00:21:12than 0 we’re going to end that function
00:21:15right there and then we’re going to also
00:21:16catch this potential error with rescue
00:21:19so we could say B
00:21:20again and then we go check age and let’s
00:21:23pass negative one inside of there let’s
00:21:25just ignore the age part there and then
00:21:27we’ll be able to come in here let’s
00:21:29indent that and say that we specifically
00:21:31want to catch an argument error and if
00:21:33it is triggered we’re going to print out
00:21:35that is an impossible age and then and
00:21:40that and you can see if it executes that
00:21:42is an impossible age prints out there’s
00:21:44a whole bunch of different ways to use
00:21:45exceptions inside of Ruby we’ve been
00:21:47doing a lot of things with strings here
00:21:49but let’s look specifically at some
00:21:50string functions and a whole bunch of
00:21:52other different things basically strings
00:21:54are just a series of characters either
00:21:55between double quotes or single quotes
00:21:58for the most part you’re going to use
00:21:59double quotes though so let’s just come
00:22:01in here and go puts add them and one
00:22:04thing it’s important that I uncover is
00:22:05if we put a hash symbol in here we’ll
00:22:07also be able to do things like perform
00:22:09addition inside of here a whole bunch of
00:22:11other different things not just variable
00:22:12names and then we could throw some back
00:22:14slashes inside of here and let’s look at
00:22:16the difference between if we would use
00:22:18double quotes versus single quotes here
00:22:20we can tell them put a single quote
00:22:22there and a single quote there execute
00:22:24that and you’re going to see here that
00:22:26the new lines indeed do work whenever
00:22:29you use double quotes as well as this
00:22:31interpolation which is right here that’s
00:22:33what that’s called if you want to know
00:22:34the technical term and whenever you use
00:22:36single quotes those do not work so
00:22:38something that’s important to remember
00:22:39and another reason why we barely ever
00:22:41use single quotes with strings a here
00:22:44doc is normally used whenever you want
00:22:46to use a multi-line string and you want
00:22:47to continue using all of the new lines
00:22:50and things so let’s just say multi-line
00:22:52string and how this would be created as
00:22:54you’d put these two carrot symbols here
00:22:56GOM and then you could go something like
00:22:58this
00:22:59is a very long string that contains
00:23:05interpolation like and then you put a
00:23:08hash symbol inside of there and say
00:23:10something like four plus five just to do
00:23:12that and then put some new lines inside
00:23:14of here just to prove that the new lines
00:23:16work and then you would end this
00:23:17multi-line string with the eom at the
00:23:19end you could then come in
00:23:20and print that out multi-line string and
00:23:23execute in there it is you can see all
00:23:25of those different lines and how they
00:23:27kept the new lines and everything all in
00:23:29order with that multi-line string let’s
00:23:31go and create a couple different strings
00:23:32here we can say first name is equal to
00:23:34Derrick and last name is equal to anise
00:23:39you can combine or concatenate strings
00:23:42by using a plus sign so we could go a
00:23:44full name is equal to and first name
00:23:47plus last name I could go in here and
00:23:50create another one say middle name is
00:23:52equal to just and you could also combine
00:23:55strings with interpolation so we could
00:23:57say full name is equal to hash symbol
00:24:01and first name and middle name and then
00:24:04last name there we are
00:24:05we could also come in here and check if
00:24:07a string contains a string and let’s
00:24:09just use puts here and we go full name
00:24:12and then we would say include with a
00:24:14question mark and what you’re looking
00:24:16for inside of that string maybe we got a
00:24:18little bit of an error let’s just throw
00:24:19new and underscore inside of there there
00:24:22we are there you can see true came back
00:24:24for that function and we could also get
00:24:26the number of characters that are inside
00:24:28of that string there’s 18 to all kinds
00:24:30of things let’s say that we wanted to
00:24:32count the number of vowels inside of
00:24:33here so just go Vale’s and then go full
00:24:37name dot count and everything you put
00:24:39inside of here is going to count where
00:24:41it’s going to find all of them so a e I
00:24:44owe you and let’s convert that to a
00:24:46string we could also do the same thing
00:24:48to find all the consonants so throw that
00:24:51there consonants and if we’re going to
00:24:53find the opposite of what we’re
00:24:55searching for so anything except what we
00:24:57have listed here we’re just going to put
00:24:59in a caret symbol like that we could
00:25:01check that a string starts with another
00:25:04string puts whole name start with and
00:25:08then we could throw in last name we
00:25:10could also come in and get the index
00:25:12inside of here or the character number
00:25:15inside of here for a match so we’ll go
00:25:18full name again and index and then what
00:25:21we’re searching for specifically worked
00:25:23out to a string and there you can see
00:25:24all those print it out for us there’s a
00:25:26ton of different functions inside of
00:25:28here
00:25:28we could also come in and check for
00:25:30equality let’s go and just delete this
00:25:33puts so if we want to check a equal to a
00:25:36then we can go a equal to a you can see
00:25:40that comes back is true we could also
00:25:42perform a check to see if something is
00:25:44the exactly the same object so it puts
00:25:47if we want it to use a double quote
00:25:49inside of here we’d have to back slash
00:25:51that double quote like that then we
00:25:53could put a and then let’s back clash
00:25:55the double quote that allows it to be
00:25:57contained inside a double quotes and
00:25:58then equal question mark back slash
00:26:01double quote a back slash double quote
00:26:03let’s concatenate that and then let’s
00:26:05actually do operation so we’ll go a and
00:26:09then equal a again and to string and
00:26:12there you can see that they show up is
00:26:13false however if we went in and typed in
00:26:16puts and say first name dot equal and
00:26:19then first name inside of there in that
00:26:21situations going back with a value of
00:26:23true because they are the same exact
00:26:25object we could also uppercase every
00:26:27single thing so just type in uppercase
00:26:30we could lowercase everything and then
00:26:32there’s also swap case you can see
00:26:34exactly what those do there you go let’s
00:26:36say that we then went into our full name
00:26:39and we just put in a whole bunch of
00:26:41spaces inside of here for no good reason
00:26:43except that we want to show exactly how
00:26:45to eliminate that whitespace so full
00:26:47name again we could then strip off all
00:26:50the left whitespace by going full name
00:26:53equal to full name dot L strip and we
00:26:57would be able to also delete any white
00:27:00space on the right by changing this to
00:27:02our strip or we could delete all the
00:27:04white space altogether by just making
00:27:06that strip and just to do a little bit
00:27:08more completions type of thing why don’t
00:27:10we cover formatting strings we could
00:27:12also go puts full name or justification
00:27:15and this is going to give us 20 spaces
00:27:18then full name plus everything else is
00:27:20going to come out to 20 spaces and let’s
00:27:22say any additional spaces we have of
00:27:24those 20 we want to put dots inside of
00:27:26there we could do that and that’s going
00:27:28to be right justified we’d also be able
00:27:30to do left justified as well as Center
00:27:34and it’s easier just to show you exactly
00:27:36what this looks like rather than trying
00:27:39to explain it and there you can say here
00:27:41we have right justified here we have
00:27:42left justified in here we have Center we
00:27:45could all
00:27:45so chop off the last character by just
00:27:48typing in chop and we could also come in
00:27:51and use chomp which we covered
00:27:53previously by default is going to
00:27:54eliminate a newline if it exists and if
00:27:57there is no new lines going to do
00:27:59nothing and if we want to delete the
00:28:00last two characters for example we would
00:28:02actually put whatever specifically one
00:28:05that eliminated and there you can see
00:28:06chop just cut off the last letter right
00:28:09there and chomp cut off both of the
00:28:11letters that we defined right there give
00:28:12ourselves a little bit more room here
00:28:13you could also have it delete specific
00:28:16characters that you’d like deleted so
00:28:18let’s just go full name delete and it’s
00:28:21going to delete every occurrence of a in
00:28:23that situation and we’d also be able to
00:28:25go and create an array from a string by
00:28:28just going full name and calling split
00:28:31and in this situation we’re just going
00:28:33to put two symbols there which basically
00:28:34means it’s going to just split every
00:28:37where there is a new character or you
00:28:41can split everywhere there is a space
00:28:43for example and let’s just copy this and
00:28:45the difference you’re going to see here
00:28:47in a moment here we’re going to put a
00:28:48space inside here you could also put a
00:28:50comments out there if you’d like if we
00:28:51execute this is the one with just going
00:28:54to show the each individual part of the
00:28:56string in separate parts of the array
00:28:58get more into arrays here in a second
00:29:00and then here you can see it’s split
00:29:01everything based off spaces and up here
00:29:03you can see that it eliminated all the
00:29:05A’s that we also covered and just to
00:29:07reiterate this a little bit you’re going
00:29:09to be able to perform string conversions
00:29:11if you want to convert a string into an
00:29:13integer
00:29:13you just put two underscore I going to
00:29:16convert it into a float to underscore F
00:29:18and if you wanted to convert it into a
00:29:20symbol which we’ll talk about a little
00:29:21bit later to underscore symbol and then
00:29:24finally if you’d like to see the most
00:29:26common backslash is you’re going to be
00:29:28able to use there they are okay so if
00:29:31everything is an object inside of a ruby
00:29:32we might as well talk about objects now
00:29:34in object oriented objects using classes
00:29:40and just like everyday objects every
00:29:42object is going to have attributes which
00:29:44we are going to call instance variables
00:29:46as well as capabilities which we are
00:29:48going to call methods now let’s say that
00:29:50we have a class called
00:29:52animal inside of it you can put
00:29:54and initialize function and this is
00:29:56going to be called anytime a new animal
00:29:58object is going to be created and this
00:29:59is where you would normally put default
00:30:01values here we’re just going to say
00:30:03creating a new animal and and that of
00:30:06course I’m going to show you really easy
00:30:08ways to create setters and getters
00:30:10inside a ruby but for now I’m just going
00:30:13to create a setter and getter let’s say
00:30:14it gets passed a new name if you wanted
00:30:17to set an instance variable inside of
00:30:19Ruby you would put the @ symbol and then
00:30:21name don’t need to do anything else with
00:30:23it and this is going to assign that
00:30:25value like that and of course we’re
00:30:26going to want to put end there as well
00:30:28we could then define a getter and you
00:30:31just put @ name and that’s going to
00:30:33automatically return that and put end
00:30:35another way you could come in here and
00:30:37get a value is def name and then just
00:30:40put name inside there like that and
00:30:42another way you could provide a setter
00:30:44is go def name equal to new underscore
00:30:47name and one of the reasons we use
00:30:49getters and setters is to make sure that
00:30:51bad data never gets in so let’s say we
00:30:53want to make sure that the name that is
00:30:55passed in is not a numeric number we
00:30:58want to make sure we’re using characters
00:31:00here we can just go numeric like that
00:31:02and if it is a numeric we’re going to
00:31:04say name can’t be a number something
00:31:07like that else we’re then going to
00:31:10assign the value that was passed in
00:31:12there to our name instance variable and
00:31:13here we will end the if and here we will
00:31:16end the setter method and then finally
00:31:18we’re going to end our class so that’s
00:31:21how that all structures out now after
00:31:23we’ve created this class we’ll be able
00:31:24to come in here and create a animal
00:31:26object so let’s say we want to create an
00:31:28object called cat we would just call
00:31:30animal and new and that’s going to
00:31:32create that animal object we could then
00:31:34go cat set name and pass in a name for
00:31:37it we could then get the name of it cat
00:31:40get name the alternative way of getting
00:31:43name will just be cat and then just
00:31:45typing a name which is normally what you
00:31:47would use like I said I’m going to show
00:31:48you a shortcut for generating getters
00:31:50and setters in a second and in an
00:31:51alternative way of actually setting the
00:31:54name just be this and then let’s say we
00:31:56want to type in this new name puts cat
00:31:59name again and there you can see
00:32:00creating a new animal and that’s the
00:32:02initialize function it’s going to be
00:32:04called every single time
00:32:05new animals created and you can see all
00:32:07the different ways that we are using to
00:32:09set the name and get the name and
00:32:10exactly how they work pretty much
00:32:12exactly the same when I go and create
00:32:14the new and improved dog class and here
00:32:17I’ll show you the shortcut for creating
00:32:18all your getter functions you’re never
00:32:20going to type this in though because I’m
00:32:21going to show you a way to generate them
00:32:23all named in this situation is a symbol
00:32:25which like I said we’ll talk about a
00:32:26little bit later on and let’s say you
00:32:28wanted to also have a weight instance
00:32:31variable inside of here those are going
00:32:32to create all of your getter functions
00:32:34automatically if you wanted to create
00:32:35all of your setter functions you just
00:32:37again list all these different instance
00:32:40variables you want to have and there you
00:32:42go or you could create all your getters
00:32:44and setters with one statement by just
00:32:46typing attr accessor which is normally
00:32:49what you’re going to do and then type in
00:32:51your instance variables again and wait
00:32:53there you go now we could come in here
00:32:55and define another function called bark
00:32:57which is just going to return a generic
00:33:00bark and close that and then close your
00:33:02class now we could create a dog dog
00:33:04object like that do that up so we can
00:33:07see it all at one place could then go
00:33:08Rover name just to prove that we have
00:33:11our getters and setters
00:33:12just call him Rover output Rover on the
00:33:15screen with Rover name and there you can
00:33:17say Rover prints out on the screen just
00:33:19like we set it now whenever we inherit
00:33:21from another class you get all of the
00:33:23methods as well as instance variables
00:33:24automatically so let’s go and create
00:33:27another class and we’ll call this German
00:33:29Shepherd and how you inherit from dog in
00:33:32this situation is just putting that
00:33:33carrot symbol in there with dog and of
00:33:35course you would be able to come in here
00:33:36and inherit everything but also
00:33:38overwrite so let’s say we went have loud
00:33:41bark print out instead of generic bark
00:33:43here you can do that and it close off
00:33:45your class everything automatically goes
00:33:47through let’s say we create a German
00:33:49Shepherd called max German Shepherd
00:33:51there he is again type new and you’ll be
00:33:54able to come in here and define name
00:33:56even though you didn’t define it inside
00:33:58of the German Shepherd class because it
00:34:01was inherited and when we talk about
00:34:03printf printf is going to be able to do
00:34:05some formatted printing for us so let’s
00:34:07say we want our strings inside here
00:34:08strings is going to be a percentage sign
00:34:10and s now we can go goes percentage sign
00:34:13s and then we
00:34:14want to throw a new line inside of there
00:34:16we would then be able to go max name and
00:34:19Max bark and if we execute it you can
00:34:22see everything works out now let’s talk
00:34:24about modules modules are made up of
00:34:26methods and instance variables just like
00:34:28classes but they can’t be instantiated
00:34:31which means you can’t turn them into an
00:34:32object we can use most common reason
00:34:34you’re going to use these is the to add
00:34:36functionality to a class because we’re
00:34:38only going to be able to inherit one
00:34:40class when we’re creating a class but we
00:34:42will be able to inherit multiple modules
00:34:45let’s start off by creating a couple
00:34:48modules here we’re going to create one
00:34:50called human and how we create a module
00:34:52is just go module and I’m going to call
00:34:54this human remember if we want to
00:34:56automatically have our getters and
00:34:57setters set up for us access err
00:35:00there we go and then let’s say we have
00:35:02name height and weight there we go we
00:35:05could then define a function inside of
00:35:07it and if we wanted to output a specific
00:35:10name for our specific object we’d use
00:35:13self just like you use this and many
00:35:16other programming languages and then we
00:35:17could just have the name show up there
00:35:19and runs close that off and just like
00:35:22everything else to close off the module
00:35:24we put an end there and I’m going to
00:35:26call this human RB is the name save it
00:35:29and let’s go and create another one this
00:35:31module is going to be called smart then
00:35:33we’ll put a function called act smart
00:35:35inside of it and whenever that’s called
00:35:37it’s going to return e equals mc-squared
00:35:40for example and we’ll close that off and
00:35:44we’ll close off our module and we’re
00:35:45just going to call this smart RB save it
00:35:47I’ll jump back over into our regular
00:35:49class we’ve been using here or regular
00:35:51file now if we want to allow access to
00:35:54those modules we’re going to have to go
00:35:56require relative at the top of our file
00:35:59here and then type in what we want and
00:36:01has to be in the same directory of
00:36:02course relative again and we’re also
00:36:05going to put smart inside of here we
00:36:07could then create another module just
00:36:09create a whole bunch of modules and this
00:36:11one’s going to have a function called
00:36:12make sound and whatever it’s called it
00:36:15is just going to print out girl on the
00:36:18screen and that and that this is just a
00:36:20show you can have modules in different
00:36:22places but you normally want them in
00:36:23separate files it’s just easier to work
00:36:25with
00:36:25we would then be able to create a dog
00:36:27class inside of here and if we want to
00:36:30include that module we just go include
00:36:32animal and end and you’ll see here we
00:36:35can go Rover is equal to create a new
00:36:37dog object like that and then Rover and
00:36:40we could say make sound and there you
00:36:42can see ger prints out on the screen we
00:36:44could also create a class called
00:36:46scientist and we could include our human
00:36:50module inside of here just by going
00:36:52include human we could also include our
00:36:55smart module inside of here and if you
00:36:57want your module to supersede so that if
00:37:01a function is in both this class as well
00:37:05as the module you are taking it from you
00:37:08would use prepend instead of include and
00:37:10we’ll go smart here then what we’re
00:37:12going to do is we’re going to define a
00:37:14function called act smart but remember
00:37:17our function in smart is going to
00:37:19supersede this one even though we
00:37:21created it inside of here because we
00:37:22used prepend instead of include and in
00:37:24this one we’ll put a different version
00:37:26MC let’s say squared like that just you
00:37:29can see it and then we’ll close off our
00:37:31class again now you can see if we create
00:37:34Einstein is equal to scientist dot new
00:37:38and we go and create a name Forum and
00:37:42make that name is equal to Albert like
00:37:45that we can print that out
00:37:47Einstein just like that and we could
00:37:49also go Einstein call the run function
00:37:52inside of the human just to show that
00:37:55that works and then go Einstein name
00:37:58says Einstein dot act smart
00:38:02save that and execute you can see right
00:38:04here equals mc-squared shows up instead
00:38:06of this and the reason why is we use
00:38:08prepend right there and if you look up
00:38:11here you can see the version that
00:38:12actually prints out on the screen that’s
00:38:14a way of using modules and classes and
00:38:16objects and all a bunch of other
00:38:17different things something else that no
00:38:19doubt will come up people think about is
00:38:21polymorphism and works a little bit
00:38:23different inside of Ruby let’s say we
00:38:25have a class called bird and it has a
00:38:29tweet method tweet what we would do here
00:38:31here is so that you know statically
00:38:33typed languages use something called
00:38:35duck typing to achieve a version of
00:38:38polymorphism and the reason why they do
00:38:40that is languages like Ruby pay less
00:38:42attention to the class type versus the
00:38:44methods that can actually be called from
00:38:46an object so let’s say we have bird and
00:38:48tweet just like we said what we would do
00:38:50here is have bird type and we would call
00:38:54tweet on that so the actual object
00:38:57that’s being passed in here we would
00:38:59then call it specific version of tweet
00:39:01you’re going to see here in a second
00:39:02what that looks like so we’ll go end and
00:39:03end this class as well then we’re going
00:39:05to create something else another object
00:39:08called Cardinal or another class anyway
00:39:10it’s going to inherit from bird and then
00:39:12we’re going to put tweet inside of it as
00:39:15well in this situation we’re going to go
00:39:16and print tweet out on the screen when
00:39:19that’s called and then let’s create
00:39:20another one not a class called parrot
00:39:23also going to inherit from the bird
00:39:25class it’s also going to have its own
00:39:27version of tweet make sure that’s
00:39:29lowercase puts and whenever it’s called
00:39:32it’s going to squawk there we are now we
00:39:34can create a generic bird which is going
00:39:37to be of bird type and what we’ll be
00:39:40able to do to simulate the polymorphism
00:39:42is called tweet and then pass in a
00:39:45cardinal in this situation and new you
00:39:49know you can go and create it create the
00:39:52Cardinal object outside of here of
00:39:54course and then we’ll go to eat and then
00:39:56in this situation we’ll call parrot no
00:39:58and if we execute that you’re going to
00:40:00see the tweet tweet prints out for our
00:40:02Cardinal which we have right here while
00:40:03squawk prints out for the parrot right
00:40:05there
00:40:06so there’s an example of duck typing and
00:40:07polymorphism inside of Ruby now I’m just
00:40:10going to briefly cover symbols now
00:40:12symbols inside of Ruby are basically
00:40:16strings that cannot be changed and
00:40:18you’re normally going to use them to
00:40:20either conserve memory or to speed
00:40:21string comparison and you would usually
00:40:23use a symbol versus a string whenever
00:40:25you need a string that’s value doesn’t
00:40:28need to change as well as you need a
00:40:30string in which you do not need access
00:40:32to string methods they’re very commonly
00:40:34used in hashes for keys and so forth
00:40:36again we’re going to get into hashes in
00:40:38a second symbol basically looks like
00:40:39this and its value is whatever it is
00:40:42right there
00:40:43let’s just go and print it out on the
00:40:44screen just so you can sort of see this
00:40:45so let’s go puts and dark and then let’s
00:40:50bring a couple of these guys in here
00:40:52five those and also let’s convert it to
00:40:55a string just to see what it looks like
00:40:57and if we come over here and execute
00:40:58you’re going to see Derek Derek
00:41:00symbol which is the class type shows up
00:41:02there as well as a unique object ID and
00:41:04like I said we’re going to get more into
00:41:06those as time goes by but you may also
00:41:08remember a symbol here which you use
00:41:11previously in which we automatically
00:41:13generated our getters and setters inside
00:41:15of our function look like that remember
00:41:18type in weight as well symbols are used
00:41:21a lot inside of Ruby and like I said
00:41:22before they’re very often used as keys
00:41:25inside of hashes since we mentioned
00:41:27hashes won’t we talk about something
00:41:29that’s very similar first called an
00:41:30array well we have already seen a raise
00:41:32let’s go and create a couple arrays here
00:41:34a couple different ways you can create
00:41:35an array you could call array new like
00:41:37that you could create an it different
00:41:40array by going array dot new and then
00:41:44saying that we want five spaces in our
00:41:46array or five little slots where we can
00:41:48put stuff in that situation the default
00:41:50value is going to be nil which means
00:41:52pretty much not a value and we create an
00:41:55array also by going array new and saying
00:41:58that we want five spaces set aside and
00:42:01the default value in this situation
00:42:03would be empty and we could also go
00:42:05create an array by actually passing
00:42:07values and you can store anything in an
00:42:09array you can store one you can put a
00:42:11string inside of there if you’d like
00:42:12there’s three or you could put float
00:42:16inside of there it’s not going to bother
00:42:17anything and let’s say we want to come
00:42:19in here and print these out on the
00:42:20screen just to see what they look like
00:42:21there you go there’s empty empty empty
00:42:24this is actually where we have nil which
00:42:26is not a value and here we have empty
00:42:29and here you can see that we printed out
00:42:30all the different values that we stored
00:42:32in our forth array now with arrays they
00:42:35are going to start off storing things in
00:42:37an index of zero which is very common so
00:42:40if we just typed in two here you can see
00:42:42the three shows up but that’s zero one
00:42:44two that’s how we’re going to get all
00:42:46those different values inside of our
00:42:47array we would also be able to come in
00:42:49and
00:42:50earn to values starting at the second
00:42:53index for example by just putting two
00:42:55and two right there and I’m going to put
00:42:57joint inside of here so we’ll join all
00:42:59of our results together and separate
00:43:01them with a comma and a space and there
00:43:03you can see that’s how that worked out
00:43:04we could also come and return values by
00:43:08doing values underscore at and then
00:43:11inside of here we could list we want the
00:43:13zero index the one index in the three
00:43:16for example and once again we’re going
00:43:18to join those together with a common
00:43:19space and put a dot inside of there
00:43:22because that is a method execute and
00:43:24there you can see it returned those
00:43:26nicely for us we would also be able to
00:43:27come in here and add a value to the
00:43:29beginning of our array by using on shift
00:43:32and then whatever value we want to put
00:43:34in there let’s say we want to put zero
00:43:35inside of there we could also go and
00:43:37remove the first item in an array by
00:43:39going shift and then there’s not going
00:43:41to be anything because it’s just
00:43:42removing an item we could add one
00:43:43hundred and two hundred for example to
00:43:46the end of array by just going push and
00:43:48play around with these on your own to
00:43:50see exactly how they work and that’s
00:43:52going to add two values to the end while
00:43:55if we would go in and type in pop that’s
00:43:57going to remove one item from the end of
00:43:59our array we could also come in here and
00:44:01concatenate or join two arrays we could
00:44:05create a brand new right here if we’d
00:44:06like so we’d say we wanted to put 10 20
00:44:08and 30 that’s going to add that to the
00:44:11very end of our array and there’s a
00:44:13whole bunch of different methods we
00:44:15could use if we wanted to get the size
00:44:17rate for and type in size put our braces
00:44:21there and let’s say we want to convert
00:44:22that to a string there you go ray size
00:44:24is equal to eight we could also check
00:44:26that an array contain the value so it
00:44:28contains 100 for example and here
00:44:31instead of size we’re going to put
00:44:33include and question mark and then
00:44:35whatever you’re searching for 100 for
00:44:37example we could check how many of those
00:44:39matches we have so how many 100s for
00:44:42example and then take the size out of
00:44:44here and instead put count inside of
00:44:47there and specifically what we’re
00:44:48looking for
00:44:49and we could also check to see if the
00:44:50array is empty by just going that and
00:44:53then we’re going to type in empty
00:44:54question mark 4.2 a string and there you
00:44:58can see all those come back we could
00:45:00worse just like I showed you with join
00:45:01before convert our array into a string
00:45:04puts array for dot join and then however
00:45:08you want those combined into your string
00:45:09P is pretty cool because it’s going to
00:45:11allow us to print out the entire array
00:45:14on the screen in just one way which is
00:45:16kind of neat and we could also print out
00:45:18our whole entire array in a loop
00:45:20situation by putting each inside of
00:45:22there and we saw this before at symbol
00:45:24let’s say we want each of these
00:45:26individual values in our array to
00:45:28temporarily be stored in a variable
00:45:30called value and then we could go puts
00:45:32and value as it’s suiting through there
00:45:34and then end that and if we execute all
00:45:37those things show up right there on our
00:45:39screen so pretty cool so that’s pretty
00:45:41much everything you want to know about
00:45:42using arrays while we look at a close
00:45:45cousin to array called hashes and a hash
00:45:48is just a collection of key value pairs
00:45:50so let’s go and create ourselves a
00:45:53number hash for example we would use
00:45:55curly braces in this situation and first
00:45:57you’re going to have your key instead of
00:45:59indexes you have keys and then you’re
00:46:01going to put that little symbol right
00:46:02there and then whatever you want the
00:46:04value to be associated with that and
00:46:05then you can put a comma and then let’s
00:46:07go and say we want another one to be
00:46:09called golden put that little symbol
00:46:11there 1.618 and then let’s say we want
00:46:14another one to be e little symbol 2.718
00:46:18and that is how you create a hash inside
00:46:21of Ruby now let’s go and use them
00:46:22we could go puts to get the actual value
00:46:25number hash and let’s just pass in well
00:46:29make sure you put square brackets here
00:46:30square brackets and whatever the key is
00:46:33and that’s going to print that out on
00:46:34the screen for you so we’re just using
00:46:36keys instead of indexes which are
00:46:37sometimes easier to use say we want to
00:46:39create a superhero type of hash and of
00:46:42course you could have these all be on
00:46:43separate lines just so they show up
00:46:44easier on your screen and go like that
00:46:46do whatever you want and of course
00:46:48you’re going to be able to call
00:46:49superheroes again and pass in Clark Kent
00:46:52for example make sure you have the key
00:46:54exactly the same though and there that
00:46:56shows up a Superman you’ll be able to
00:46:58come in and add an individual new key
00:47:00and value to your hash by just going
00:47:03superheroes and then defining whatever
00:47:05you want the key to be right like that
00:47:08automatically going in and adding in a
00:47:11new value for that key there it is it’s
00:47:12in there now something else is you could
00:47:15add a default key value which is very
00:47:17useful so that if the user goes and
00:47:19looks for something that doesn’t exist
00:47:20inside of your hash it’s going to print
00:47:23out something useful like no such key if
00:47:26it can’t find anything which is like I
00:47:28said pretty useful so you go in here and
00:47:31say I want to get whatever is in sample
00:47:34hash with the key dog it doesn’t exist
00:47:37and you see no such key comes up we
00:47:39could then come in and create some super
00:47:41heroines hashes right like this you
00:47:43could then combine these with update by
00:47:46just putting in super heroines instead
00:47:48and whenever you’re using update this is
00:47:50considered a destructive merge which
00:47:52means if there’s any duplicates they’re
00:47:53going to be eliminated you could instead
00:47:55use merge which would be a non
00:47:58destructive which would keep all the
00:48:00keys and values even if they matched
00:48:01normally you’re going to use update
00:48:03because you wouldn’t want any
00:48:04duplication inside your hash I’ll just
00:48:05cost confusion now if you want to go
00:48:07through exactly how to print all of
00:48:08these out we would just go superheroes
00:48:11and we would use each again do you could
00:48:14get the keys and values by just putting
00:48:16in key and value and then close that and
00:48:20then we’ll go puts and then we go key
00:48:22and we need to convert that to a string
00:48:24plus put some separation between these
00:48:26and a value and end execute you can see
00:48:30all of them print it out real nice
00:48:31and of course there’s a whole bunch of
00:48:32different functions available for hashes
00:48:34as well we could come in here and check
00:48:36that our superheroes hash has the key of
00:48:40Lisa Morel like this also be able to
00:48:42come in and check if we have a value by
00:48:44passing in a value so there’s Batman and
00:48:47you can see right there’s Batman this is
00:48:48the key that’s the value and of course
00:48:50we could check if it’s empty as well as
00:48:52whatever the size is for it if we
00:48:54execute that you can see all those come
00:48:56out there and the only other thing I
00:48:57could think of is how would you delete a
00:48:59key you would go in here and go
00:49:01superheroes and delete and you could
00:49:04pass in whatever would be a matching key
00:49:06and that’s going to delete that for you
00:49:08if we wanted to double check that that
00:49:09actually was deleted
00:49:11in here see if the actual value for the
00:49:14size changed and yes you can say five
00:49:16and four so those are all the main
00:49:17things you’d ever want to know about
00:49:19using hashes inside of Ruby so why don’t
00:49:22we go and check out enumerables now a
00:49:25class that includes the enumerable
00:49:27module is going to gain collection
00:49:28capabilities sort of like we saw with
00:49:30arrays and hashes now let’s just come in
00:49:32here and create one let’s call it menu
00:49:34and let’s say include enumerable just
00:49:37like this one requirement however is if
00:49:40you’re going to inherit this you’re
00:49:42going to have to provide a function that
00:49:45is called each now we’re going to put
00:49:47inside of here all the different things
00:49:48that we’re going to have in this
00:49:50enumerable so if we have a menu we’re
00:49:52going to have pizza and you’re going to
00:49:54find all these different options by
00:49:56putting yield inside of here for each of
00:49:58the possible options and salad and water
00:50:02and bread is keeping it simple courses
00:50:05is going to end with end and then the
00:50:07class will end with end will then be
00:50:09able to go and you options and create a
00:50:12new menu just by using new just like
00:50:14we’re used to we can in cycle through
00:50:16all those by going menu options and
00:50:18guess what each do again just like we’ve
00:50:21seen many times temporarily held in item
00:50:24and we could put puts and we could have
00:50:26an automated sort of waitress here would
00:50:29you like and then put in an item and put
00:50:32end there we go and ask would you like
00:50:34all the different things on our menu we
00:50:36could then check to see if we have a
00:50:38pizza for example so menu options go
00:50:41find then inside of here well make sure
00:50:43you use curly brackets in a situation it
00:50:46would say item and we would check if any
00:50:48of these items is equal to pizza and if
00:50:51it does find a match it’s going to print
00:50:52that out we do all kinds of weird things
00:50:54let’s say that we wanted return items
00:50:56that are 5 letters in length we could
00:50:58come in here and say select and this is
00:51:01going to cycle through all these
00:51:02different options so we could say so
00:51:04item item size less than or equal to 5
00:51:08likewise we could use reject instead of
00:51:11select reject and it’s going to reject
00:51:14anything that meets those options we
00:51:16could also come in and print out the
00:51:18first item we have we could also come in
00:51:21and print out the first two by using
00:51:24take like this we could return
00:51:26everything except for the first two by
00:51:28using drop it’s not going to affect the
00:51:31enumerable in any way just going to show
00:51:33those and onwards and onwards we could
00:51:35also menu options get the minimum item
00:51:38we could also get the maximum item and
00:51:42that’s going to pend upon alphabet order
00:51:44or whether you have numbers or letters
00:51:45or whatever we’re also going to have
00:51:48sorting options and we could even return
00:51:50every single option in reverse order if
00:51:53we’d like so go reverse each just to be
00:51:56kind of silly here there we go execute
00:51:58and you can see everything prints out
00:52:00there on the screen so there is pretty
00:52:01much everything you want to know about
00:52:03enumerables and we’re pretty much done I
00:52:06figure why don’t we just cover the file
00:52:08object because it’s very useful as you
00:52:10saw before if you want to create a new
00:52:11file that you want to write to use the
00:52:13file object followed by new and then you
00:52:16would provide a name that you would like
00:52:18to have assigned to this new file you
00:52:20create I want to be able to write to it
00:52:22just put a W inside of there and there
00:52:24you go you have a file you can write to
00:52:26if it’s not created it will be created
00:52:27then you can just use puts to
00:52:30automatically go in and throw some
00:52:32information into your newly created file
00:52:35let’s just throw in three authors after
00:52:38you open it and put things in the file
00:52:40you would of course want to close it you
00:52:42want it to output everything that is an
00:52:44assigned to this file object you would
00:52:45just use read and then pass in the file
00:52:48that you wanted to pull information out
00:52:50from just check to make sure all that
00:52:52works and yes indeed it does if you want
00:52:54to open a file for appending just use
00:52:56the file object again followed by new
00:52:58and authors out and then in this
00:53:02situation instead of a double if you
00:53:03want to append to the end of the file
00:53:05you would put an A inside of there then
00:53:07come in and throw in a new author that
00:53:09you may have forgotten Danielle Steel
00:53:11for example and of course close that
00:53:14file again then if you wanted to read
00:53:15from this file again just to make sure
00:53:17that it updated you can see that
00:53:19Danielle Steel is now there this didn’t
00:53:21copy this multiple different times just
00:53:23shows that way because we have this
00:53:24other read here then let’s go and create
00:53:26another file equal to file well make
00:53:30sure that’s uppercase no and let’s call
00:53:32this one off
00:53:33there’s info out and it’s a new file
00:53:37that hasn’t been created let’s make it
00:53:39writable and in this situation we’re
00:53:41going to put a whole bunch of
00:53:42information in here so we’re going to
00:53:43have the author’s name their language
00:53:45the type of poetry or types of books
00:53:49they wrote and the number of items sold
00:53:51on our screen close our file and then
00:53:53let’s say we want to cycle through the
00:53:55data to write out sentences based off
00:53:58the data that’s stored inside of there
00:53:59well you go file open and then we could
00:54:01type in author info like this out there
00:54:05is the name of our file do we’re gonna
00:54:08be able to use do again and we could say
00:54:09each one of those lines is going to be a
00:54:11record and then we could go record each
00:54:14do we could have each line of that
00:54:16document go inside of the item here we
00:54:19could then split each line into four
00:54:21parts based off of commas so we’ll say
00:54:24name will be the first one lying will be
00:54:26the next one specialty will be the next
00:54:29one and sales will be the next one is
00:54:31equal to item this item represents the
00:54:33whole entire line and we can say chomp
00:54:36which means all that information is
00:54:37going to be split based off of commas
00:54:39and then after we split all that stuff
00:54:41we could go put and make some neat
00:54:43things let’s say name was a and we could
00:54:47say what type of book make sure you have
00:54:50the curly brace clothes there was a and
00:54:53we type in language author that
00:54:56specialized in and whatever their
00:54:59specialty was and then say something
00:55:01like they sold over and whatever the
00:55:05sales were for their books and then
00:55:07books and then after that we want to
00:55:10close this off and then close this off
00:55:12and if we save that and jump over here
00:55:15you’re gonna see when Shakespeare was an
00:55:17English author specialized in plays and
00:55:18poetry they sold over 4 billion books so
00:55:21there you go guys there is a heck of a
00:55:22lot of information about Ruby please
00:55:25leave your questions and comments below
00:55:26otherwise till next time