Ruby/C3/for-and-each-Looping-Statements/English
Title of script: Looping Statements
Author: Anjana Nair
Keywords: for, each, loop, Ruby, video tutorial
Visual Cue | Narration |
Slide 1 | Welcome to the tutorial on for and each Loops in Ruby. |
Slide 2 | In this tutorial we will learn
|
Slide 3
System Requirements |
Here we are using
|
Slide 4
Pre-requisites |
To follow this tutorial, you must have Internet Connection.
You must also have knowledge of Linux commands, Terminal and Text-editor. If not, for relevant tutorials, please visit our website. |
Now let me explain what is referred to as a “loop” in Ruby. | |
Slide 5
What is a loop? |
A loop is a command or set of commands that are executed a fixed number of times. |
Slide 6
Looping statements |
Ruby has the following main looping statements.
|
In this tutorial we will learn how to use the for and each looping constructs. | |
Switch to gedit | Click on “dash-home” and type gedit to invoke the editor. |
Switch to terminal | Similarly, click on “dash-home” and select Terminal to open the terminal. |
Type mkdir ttt and press Enter | In the terminal, type “mkdir ttt”. |
Type cd ttt and press Enter | Then “cd” into that directory. |
Type mkdir ruby-tutorial and press Enter | Then type “mkdir ruby-tutorial”, |
Type cd ruby-tutorial and press Enter | And then “cd ruby-tutorial”. |
Type mkdir looping-statements and press Enter | Then type “mkdir looping-statements” |
Type cd looping-statements and press Enter | And then “cd looping-statements” |
Now we have created the requisite folders.
| |
Slide 7
“for” loop |
The syntax of the “for” loop in Ruby is as follows:
for “variable” in “a collection of objects” ruby code end Let us try to understand it with an example. |
Switch to gedit where you have already opened the file “for-loop.rb” with the for loop code typed inside. | Create a new file in gedit as shown in the basic level Ruby tutorials.
And name it “for-loop.rb” |
I already have a working example of the “for” loop.
You can type the code as we go through this example. Pause the tutorial while you do this. | |
Highlight “for” | I have declared a “for” loop in this example. |
Highlight (1..20) | We have a set of numbers 1 to 20. |
Highlight “for i in (1..20)” | We declare a variable called “i” within the “for” loop. |
Continue the highlight | The variable “i” gets initialised to the first element in the set of numbers 1 to 20. |
Continue the highlight | The “for” loop declaration causes the code to iterate over each element in the set 1 to 20. |
Highlight “puts” | The “puts” method declared within the “for” loop is responsible for generating the output. |
Switch to the terminal and type
ruby for-loop.rb
|
Now open the terminal and type
“ruby for-loop.rb”
|
Analyzing the output | The output will be an array of numbers 1 to 20. |
In this example, we declared a “for” loop for an inclusive range.
It included all numbers from 1 to 20. | |
Next we shall look at implementing the “for” loop for a non-inclusive range. | |
Switch back to the same file. | Let's get back to the same file we were looking at. |
Continue to type the next part of the code. | |
Highlight “for i in (1...20)” | Non-inclusive means it will not include the last element in the collection of objects. |
Continue the highlight | Here a “for” loop is implemented for a non-inclusive range of numbers 1 to 20. |
Continue the highlight | You will notice shortly that the number 20 will not be printed in the output. |
Highlight the 3 dots only | The last digit does not get included when you have 3 dots between numbers. |
Switch to the terminal and type
ruby for-loop.rb
|
Now open the terminal and type “ruby for-loop.rb”
|
Analyzing the output | The output will still be an array of numbers but will not include the number 20.
<Pause> |
Now, you should be capable enough to write your own “for” loop. | |
Slide 8
“each” loop |
The syntax of the “each” loop in Ruby is as follows:
“a collection of objects”.each do |item| ruby code end
|
Switch to gedit where you have already opened the file “each-loop.rb” with the each loop code typed inside. | Create a new file in gedit as shown in the basic level Ruby tutorials.
|
I already have a working example of the “each” loop.
| |
Highlight “for” | I have declared an “each” loop in this example.
We have a set of numbers 1 to 20. |
Highlight “for i in (1..20)” | We declare a variable called “i” within the “each” loop. |
Continue the highlight | The “i” variable gets initialised to the first element in the set of numbers 1 to 20. |
Continue the highlight | The “each” loop declaration causes the code to iterate over each element in the set 1 to 20. |
Highlight “puts” | The “puts” method declared within the “each” loop is responsible for generating the output. |
Switch to the terminal and type “ruby each-loop.rb” | Now open the terminal and type “ruby each-loop.rb”
|
Analyzing the output | The output will be an array of numbers 1 to 20. |
In the above example we declared an “each” loop for an inclusive range.
| |
Next we shall look at implementing the “each” loop for a non-inclusive range. | |
Switch back to gedit | Let's get back to the same file we were looking at. |
Continue to type the next part of the code. | |
Highlight “for i in (1...20)” | Non-inclusive means it will not include the last element in the collection of objects. |
Continue the highlight | Here a “each” loop is implemented for a non-inclusive range of numbers 1 to 20. |
Continue the highlight | You will notice shortly that the number 20 will not be printed in the output. |
Highlight the 3 dots only | The last digit does not get included when you have 3 dots between numbers. |
Switch to the terminal and type
ruby each-loop.rb
|
Now open the terminal and type “ruby for-loop.rb”
|
Now you should be capable enough to write your own “each” loop.
<Pause> | |
Slide 9 | How would we determine which looping contruct to choose?
Let us try to recall the “for” loop construct. |
Open “for-loop.rb” | In the first example, we iterated over a set of numbers 1 to 20 using “for”.
|
Highlight “for number in (1..20)” | When you invoke the “for” loop, Ruby is actually calling the “each” method behind the scenes.
|
Since the call to “for” in turn calls “each”, it is preferable to use the “each” loop instead.
<Pause> | |
Slide 10 | <<Pause>>
This brings us to the end of this Spoken Tutorial. Let's summarize. |
Slide 11
Summary |
In this tutorial we have learnt
|
Slide 12
Assignment |
As an assignment
Write a ruby program using
|
Slide 13
About the Spoken Tutorial Project |
Watch the video available at the following link.
It summarises the Spoken Tutorial project. If you do not have good bandwidth, you can download and watch it. |
Slide 14 | The Spoken Tutorial Project Team :
Gives certificates to those who pass an online test For more details, please write to contact at spoken hyphen tutorial dot org |
Slide 15
Acknowledgements |
Spoken Tutorial Project is a part of the Talk to a Teacher project.
It is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this Mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro. |
Slide 16
About the contributor
|
* This tutorial has been contributed by Ruby Software Pvt. Ltd
|