Ruby - Iterators

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's a lot easier. It is a lot cleaner. It is easier to see what we are trying to do and we have to write less code in the process. This is an iterator. Now it might not be obvious to you how this is iterating through numbers instead of looping. It may look like it's just looping five times.

But it's actually not and we will see that in a moment. But first, let's take a look at some of the other iterators that are very similar to this. 1.upto (5) and that will put Hello; 5.downto (1) put Hello or we can use a range. (1..5).each put Hello. Now the last one probably makes it the clearest.What we are saying is take each item in this range and do this code block. Now notice I have curly braces around my code blocks now instead of the do end that we were looking at before. This is just the shorthand syntax for it.

So you will be seeing both, both of them are code blocks. Again we will be talking about code blocks later on, but just make a little footnote, do end is a code block and the curly braces are code blocks. Same thing. I could have written it this way just as easily. Now there is more to an iterator than simply making it shorter and easier to write. There is actually a really nice thing where we have access to the values that are inside that data set while we are iterating through. Let me show you. So here is that same example. From 1.upto (5) do and now notice after the do I have a little something extra.

I have an upright bar which we call a pipe, an i and another pipe. And then i can be anything. I could make it X, I could make it num. It is just a local variable that I am going to use inside my loop to keep track of the data that is in this data set. So the first time through i is going to be equal to 1. Second time through it will be equal to two. So it will increment. As we go through it will grab that next value of i. Now you can see that I am actually outputting that there, puts Hello + and then I will take the value of i to strings so we can actually see it.
I am going to open up irb with simple prompt. Let's go ahead and create that same one. 1.upto(5) and then do and we have the upright bar pipe, right followed by an i. I am going to go ahead and use something different now so you can see that can be anything, num for example,a new line and now it's my code block. It is a code block where I have num available to me and num is the value of each element as it iterates through the dataset. So we can have puts and we will have Hello with a space +num.to_s.

Just to go ahead and make sure because it will be a number because that's what we are iterating through, right? The numbers 1 to 5. And then let's end that and you will see what it outputs. Our iterator yields that value of each of those items as it goes through into num and we can output it. Let's try a different example that doesn't use numbers. So let's say we have fruits and in our fruits array we will put a banana and let's put an apple and how about a pear? So that's just a simple array of fruits. Well now I can say fruits.each do, so for each item there I am going to declare the value as fruit.
Let's go ahead and make it something nice and common sense, right? It makes sense that each one as we go through is going to be a fruit.So as it iterates, it will pop a fruit into that value fruit and then I can say puts and let's say fruit and let's just capitalize. There we are, banana, apple and pear. So you can see, it went through each one. Now it also then gave me a return value back at the end. Don't worry about that.That's the return value for fruits. That's what it gives you back. What we are interested in is the output.

What it actually did here where we went through our loop and we iterated along each item. Now there is another form to this each I just want to show you as well. Sometimes for beginners this is easy. We say for fruit in fruits, right. This is not the same as if we had a For statement in another language something like C++ or Java, PHP. They have different kinds of For statements. It is not the same at all. This is for each item and we are basically declaring a name here, same thing.

These are equivalent. For each fruit in fruits output that fruit. So it's exactly the same thing just a different syntax of writing. If it is easier for you to remember this way, great. If you are totally hip with this code block thing and you are ready to do it, then go ahead and use this fruits.each do as a fruit and then execute whatever is in your code block. Now we still have access to those same control functions that we used with our loops. This work in iterators as well. Break, next, redo and retry. I am not going to show you how those work but redo and retry are little more useful in our iterators because now retry doesn't just say, "hey start a loop over." Retry says go back to the beginning of the set.
So if we have banana and then we have apple and then we do a retry, well now it is going to jump back to banana again and retry starting from the beginning of the list. And if we have our redo, same thing if we went banana, apple, if we do a redo, it is going to the apple again.We will need of course to have some branching conditional If statements in there to make sure that we don't just endlessly loop on one thing. But we can use redo and retry a little more effectively with iterators than we could with loops. Now there a lot of different iterators in Ruby and I am not going to try to go through each of them with you, but you can look up the documentation on them.

I want to just give you a list of what some of there are. So we have got with Integers and Floats, we saw how we could do times like 5 times 10 times. Upto, downto, there is also step and that step just basically says go from 1 to 10, but we are going to step every 2 right, so that we could go 1, 3, 5, 7. Range we can use each for each other we saw. We also can use step there. So it will go from the beginning of the range to the end of the range by whatever step we tell it, every four numbers, every five numbers. With string we also have each.
We have each line, we have each byte. With array we can use each, each index of the array or each item in the array with its index, in which case we actually get two values yielded up to the variables that we declare inside the pipes. So we actually put pipe and then one variable, and then another variable then the final pipe and we have two values we can work with. And then with the hash we have each key, each value or each pair which also yields up two values that we can work with. Now we will be working a lot with iterators and they will become very comfortable to you.
I think you will also find that you use iterators many times more than you use loops. Iterators are going to be just much more useful for you and there is a lot more things that you can do with them. Now that we have talked about the control structure and we have touched on the way the code blocks can be useful for you.
CHEERS,
Maitrey Patel

Comments

Popular posts from this blog

Ruby - Conditionals unless - case