Posts

Showing posts from May, 2017

Ruby - Iterators

Image
Greeting Folks, This article is about Ruby Control Structure for iterators.   Now iterators work a lot like loops which we just saw in the last movie.   The difference is that instead of just looping, waiting for something to happen   for us to take control and to either break out of it or to do something else,   instead with an iterator we are going to traverse a fixed set of data.   So we can kind of know where the starting point and the ending point is.   We basically want to say for each one of these things do this process, do this loop, right.   So we are going to do a code block once for each item in a set of data. Now we can accomplish that with loops.   For example here is a While loop.   Essentially what this while loop is doing is just outputting Hello five times.   Before trying to do that, there is a simpler way to do it using the Ruby syntax,   which is just to say 5.times do and then our code block.   That...

Ruby - Loops

Image
Greetings!!! Loop does exactly what you think it would.   It loops through a bit of code over and over and over again until you tell it to stop.   And the way that we define a loop in Ruby is by simply having loop space and   then word do, then the code that we want to repeat will be on a new line after that   and then on a new line once that's done, we would have the word end.   Everything from do to end would be called a code block and we are going to be   talking more about code blocks a little later on and going into depth with them,   because they're a really useful thing. This really the first time we've encountered them, so just make a mental note   this is our first code block.   Loop will take a code block and just loop through that block over and over and over again.   In fact, if we were to go to IRB  and just type in something that was a really   simple look like this, it would just keep looping and i...

Ruby - Conditionals unless - case

Image
Greetings of the Day!!! We have gone over all the basics that you will need in order to work with   Conditionals, if, else and else if will do everything that you need. However, there are a couple of convenience conditionals that we can also use   that work kind of like a shorthand.   We are going to look at four of them.   There is unless, case, the ternary operator and or/or-equals.   So let's start out by looking at unless.   It looks like this, exactly like the if statement, but instead of if it's unless.   So unless a Boolean is true, which means the same as if not true, it's the reverse. So instead of being if not x > 3, it's unless x > 3.   So unless it's true, do this block of code.   Now unless can sometimes tie you in logical knots trying to work through exactly   when it's going to be used.   So only use it if it makes a lot of sense.   If it feels right and it's convenient to use unless, but don't let ...