Ruby/C3/for-and-each-Looping-Statements/English

From Script | Spoken-Tutorial
Revision as of 15:29, 22 April 2013 by Sneha (Talk | contribs)

Jump to: navigation, search

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
  • Meaning of the term “loop”
  • The different kinds of loops in Ruby
  • Usage of “for” loop and
  • Usage of “each” looping construct
Slide 3

System Requirements

Here we are using
  • Ubuntu version 12.04
  • Ruby 1.9.3
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.
  • for
  • each
  • while
  • until
In this tutorial we will learn how to use the for and each looping constructs.


Type mkdir ttt and press Enter Before we begin, please create the following folders in your home directory,ttt, ruby-tutorial, looping-statements. please create the sub- folder as mentioned here Now we have created the requisite folders. Let see them ahead


Now we have created the requisite folders.


Let's move ahead.

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.


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


Point to the output

Now open the terminal and type

ruby for-loop.rb


and see the output.

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.


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


Point to the output

Now open the terminal and type “ruby for-loop.rb


and see the output.

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


Let us try to understand it with an example.

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.


And name it “each-loop.rb

I already have a working example of the “each” loop.


You can type the code as we go through this example.



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


and see the output.

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.


It included all numbers from 1 to 20.

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


Point to the output

Now open the terminal and type “ruby each-loop.rb


and see the output.

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 from 1 to 20 using “for”.


Execute “ruby for-loop.rb” in your terminal and see the output.


Now look at the code in gedit.

Highlight “for number in (1..20)” When you invoke the “for” loop, Ruby is actually calling the “each” method behind the scenes.


And calling “each” or “for” will generate the same output.

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
  • Usage of “for” loop
  • Usage of “each” construct
  • Reasoning behind using “each” instead of “for
  • Examples of using the above looping constructs
Slide 12

Assignment

As an assignment

Write a ruby program using

  • the appropriate loop construct
  • to create an output of even numbers
  • from a set of numbers, say 1 to 20


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 :


Conducts workshops using spoken tutorials

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

Contributors and Content Editors

Anjana, Nancyvarkey, Sneha