Ruby/C2/Ruby-Methods/English

From Script | Spoken-Tutorial
Revision as of 13:34, 26 February 2013 by Nancyvarkey (Talk | contribs)

Jump to: navigation, search

Title of script: Ruby Methods

Author:Afrin Pinjari

Keywords: method,method syntax, return, video tutorial


Visual Cue
Narration
Slide 1 Welcome to the Spoken Tutorial on Ruby Methods.
Slide 2

Learning Objective

In this tutorial we will learn
  • What is a method ?
  • Syntax for method and
  • Some examples
Slide 3

System Requirement

Here we are using
  • Ubuntu Linux version 12.04
  • Ruby 1.9.3
Slide 4

Pre-requisites

To follow this tutorial you must have knowledge of using Terminal and Text editor in Linux.
Slide 5

What is Methods

Let us now start with an introduction to methods.

A Method is a self-contained program executing a specific task.

Ruby method is very similar to functions in any other programming language.

Slide 6

Method

Method name should begin with a lowercase letter.

Methods should be defined before calling them.

Slide 7

Syntax for Method

Let us see the syntax of Method

def method_name(arguments)

ruby code

end

OR

def method_name ()

ruby code

end


Methods are defined using the keyword def followed by the method name.


The arguments specify values that are passed to the method, to be processed.


The ruby code section represents the body of the

method that performs the processing.


The method body is enclosed by this definition on the top and the word end on the bottom

This is called as method with arguments.

Slide 7 Another syntax for method is


The keyword def followed by the method name and an empty argument list


The ruby code section represents the body of the

method .

The word end marks end of method

This is called as method without arguments.

Switch to the editor – gedit. Let's see how to use a method.

I have already typed a program in the gedit editor.

Let me open it.

Point to the filename on the Title bar. Please note that our filename is method-without-argument.rb

I have saved the file inside our rubyprogram folder.

In this program we will calculate the sum of two numbers using method.
Let us go through the program.
Highlight $a=5 Here we have declared a global variable a.

And we have initialized it by assigning value of 5.

Global variable names are prefixed with a dollar sign ($).

Global variables are accessible from anywhere in the Ruby program;

regardless of where they are declared.

Highlight def add() Here we have declared a method add without any arguments.
Highlight print"Enter the second number:" Here we ask the user to enter the second number.
Highlight b=gets.to_i User will enter the value.
Highlight gets gets method gets user input from the console but in string format.
Highlight to_i So we need to convert it into integer, using to_i method.
Highlight b The converted value is stored in the variable b .

b is a local variable.

It is available only to the method inside which it is declared.

Highlight sum=$a+b Here we add the values of global variable a and variable b.

The result is stored in variable sum.

Highlight puts"Sum of#{ $a} & #{b} is #{sum}" Then we print the sum.
Highlight #{sum} This shows a way of inserting a variable within a string.

Here the content of sum is returned as a string and is

substituted into the outer string.

Highlight end end marks end of the method.
There are two types of methods.

User-defined method - that is our add method.

Pre-defined method - that is print, gets and to_i method.

Highlight add() Here we call the add method.

The addition operation will be performed and the result will be

printed.

Click on Save. Now let us click on Save.

This program will be saved in rubyprogram folder as mentioned earlier.

Now let us execute the program.

Press Ctrl+Alt+t

Switch to Terminal

Open the terminal by pressing the Ctrl, Alt and T keys simultaneously.

A terminal window appears on the screen.

To execute the program, we need to go to the subdirectory rubyprogram.
Type cd Desktop/rubyprogram<< Press Enter So let's type cd space Desktop/rubyprogram and press Enter.
Type ruby method-without-argument.rb <<Press Enter Type ruby space method-without-argument.rb and press Enter
Enter the second number is displayed.

I will enter value as 4.

type 4 and press enter

Highlight Sum of two numbers 5 and 4 is 9 We get the output as -

Sum of two numbers 5 and 4 is 9

Switch back to editor Now let us see an example of method with arguments.

I have already typed this program in the gedit editor, so let me open it.

Point to the filename in the Title bar. Please note that our filename is method-with-argument.rb

I have saved this file also inside the rubyprogram folder.

Let us go through the program.
Highlight def add(a ,b) Here we have declared a method called add.

a,b are the arguments of the method add.

Highlight return a+b Here the values of a and b are added.

And the sum is returned to the method call.

Highlight end end marks the end of method.
Highlight puts"Enter the values of a and b"

a=gets.to_i

b=gets.to_i

Here we are asking the user for input.

User will enter the values of a and b.

The values will be stored in variable a and variable b, respectively.

Highlight c=add(a,b) Here we call the method add

Then we pass the arguments as a and b.

Highlight c The value returned by the method add , after performing the addition operation will be stored in c.
Highlight puts"Sum of two number #{a} and #{b} is #{c}" Here we print the sum which is store in c.
Switch back to terminal

Type clear<<Press Enter

Lets execute this code.

Go to the terminal.

Let us first clear the terminal

Type clear and press enter

Point to the command prompt to show subdirectory rubyprogram We are already in the subdirectory rubyprogram.
Replace

ruby method-with-argument.rb << Press Enter

Now, press the up arrow key twice to get the previous command.

Replace method-without-arguments.rb with method-with-arguments.rb

Press Enter

Enter the values of a and b is displayed.

I will enter 8 and 9.

Type 8 and press Enter

Type 9 and press Enter

Highlight Sum of two numbers 8 and 9 is 17 We get the output as

Sum of two numbers 8 and 9 is 17.

<<pause>>

Now I will show you one important feature of Ruby method.
Switch back to the text editor Let's go back to the program in the text editor.
Delete the keyword return.
Click on Save Now click on Save button.
Switch back to terminal Let's execute the code.

Go to the terminal.

Replace

ruby method-with-argument.rb << Press Enter

Press the up arrow key to get the previous command.

Press Enter.

Enter the values of a and b is displayed.

I will enter 10 and 15.

Type 10 and press Enter

Type 15 and press Enter

Highlight Sum of two numbers 10 and 15 is 25 We get the output as

Sum of two numbers10 and 15 is 25.

We can see that the program is executed without any error, even after deleting the keyword return.
Switch back to the program

Highlight a+b

This is because Ruby automatically returns the value calculated in the method.

The keyword return in method is optional in Ruby.

<<Pause>>

This brings us to the end of this Spoken Tutorial.

Slide 11

Summary

Let us switch back to slides

Let's summarize

In this tutorial we have learnt

  • About Methods
  • Syntax
  • Method without arguments
  • Method with arguments
  • Returning value from method
Slide 13

Assignment

As an assignment

Write a program

  • to calculate area of a square
  • by using method and
  • by getting the input from user
Slide 14

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 15 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@spoken-tutorial.org

Slide 16


Acknowledgement

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-tutorial.org/NMEICT-Intro.

This is Afrin Pinjari from IIT Bombay, signing off.

Thank you for watching.

Contributors and Content Editors

Afrin, Nancyvarkey, Sneha