Ruby/C2/Ruby-Methods/English
Title of script: Ruby Methods
Author:Afrin Pinjari
Keywords: method,method syntax, return, video tutorial
|
|
---|---|
Slide 1 | Welcome to the Spoken Tutorial on Ruby Methods. |
Slide 2
Learning Objective |
In this tutorial we will learn
|
Slide 3
System Requirement |
Here we are using
|
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.
|
Slide 6
Method |
Method name should begin with a lowercase letter.
|
Slide 7
Syntax for Method |
Let us see the syntax of Method
ruby code end OR
ruby code end
method that performs the processing.
This is called as method with arguments. |
Slide 7 | Another syntax for method is
method .
|
Switch to the editor – gedit. | Let's see how to use a method.
Let me open it. |
Point to the filename on the Title bar. | Please note that our filename is method-without-argument.rb
|
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.
|
Global variables are accessible from anywhere in the Ruby program;
| |
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 .
|
Highlight sum=$a+b | Here we add the values of global variable a and variable b.
|
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.
|
Press Ctrl+Alt+t
|
Open the terminal by pressing the Ctrl, Alt and T keys simultaneously.
|
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 -
|
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
|
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.
|
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
|
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
|
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.
|
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.
|
Replace
ruby method-with-argument.rb << Press Enter |
Press the up arrow key to get the previous command.
|
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
|
Let us switch back to slides
Let's summarize
|
Slide 13
Assignment |
As an assignment
Write a program
|
Slide 14
About the Spoken Tutorial Project |
Watch the video available at the following link.
If you do not have good bandwidth, you can download and watch it. |
Slide 15 | The Spoken Tutorial Project Team :
Gives certificates to those who pass an online test For more details, please write to
|
Slide 16
|
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. |