Difference between revisions of "Ruby/C2/Ruby-Methods/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''': Ruby Methods '''Author:'''Afrin Pinjari '''Keywords: '''method,method syntax, return, video tutorial {| style="border-spacing:0;" ! <center>Visual Cue…')
 
 
(2 intermediate revisions by 2 users not shown)
Line 6: Line 6:
  
  
 
+
{| border=1
{| style="border-spacing:0;"
+
| '''Visual Cue'''
! <center>Visual Cue</center>
+
| '''Narration'''
! <center>Narration</center>
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 1
+
| Slide 1
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the Spoken Tutorial on '''Ruby Methods'''.
+
|Welcome to the Spoken Tutorial on '''Ruby Methods'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 2
+
| Slide 2
  
 
Learning Objective
 
Learning Objective
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn
+
|In this tutorial we will learn
  
 
* What is a '''method''' ?
 
* What is a '''method''' ?
 
* Syntax for '''method''' and
 
* Syntax for '''method''' and
* Some examples
+
* We will see  Some examples
 
+
 
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 3
+
| Slide 3
  
 
System Requirement
 
System Requirement
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we are using
+
|Here we are using
  
 
* '''Ubuntu Linux '''version 12.04  
 
* '''Ubuntu Linux '''version 12.04  
 
* '''Ruby''' 1.9.3
 
* '''Ruby''' 1.9.3
 
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 4
+
| Slide 4
  
 
Pre-requisites
 
Pre-requisites
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To follow this tutorial you must have knowledge of using '''Terminal '''and '''Text editor '''in '''Linux'''.  
+
|To follow this tutorial you must have knowledge of using '''Terminal '''and '''Text editor '''in '''Linux'''.  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 5
+
| Slide 5
  
 
What is Methods
 
What is Methods
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us now start with an introduction to '''methods.'''
+
|Let us now start with an introduction to '''methods.'''
 
+
  
 
A '''Method''' is a self-contained program executing a specific task.  
 
A '''Method''' is a self-contained program executing a specific task.  
 
  
 
'''Ruby method''' is very similar to functions in any other programming language.
 
'''Ruby method''' is very similar to functions in any other programming language.
 
 
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 6  
+
| Slide 6  
  
 
Method
 
Method
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Method''' name should begin with a lowercase letter.
+
|'''Method''' name should begin with a lowercase letter.
 
+
  
 
'''Methods''' should be defined before calling them.
 
'''Methods''' should be defined before calling them.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 7
+
| Slide 7
  
 
Syntax for Method
 
Syntax for Method
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see the syntax of '''Method'''
+
|Let us see the syntax for '''Method'''
 
+
  
 
'''def method_name(arguments)'''
 
'''def method_name(arguments)'''
Line 82: Line 70:
  
 
'''OR'''
 
'''OR'''
 
  
 
'''def method_name ()'''
 
'''def method_name ()'''
Line 92: Line 79:
  
 
'''Methods '''are defined using the keyword '''def''' followed by the '''method''' name.
 
'''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 '''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 '''ruby code''' section represents the body of the  
+
The '''method''' body is enclosed by this definition on the top and the word '''end''' at the bottom.
  
'''method''' that performs the processing.'' ''
+
This is called as '''method with arguments'''.
 
+
 
+
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.
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 7
+
| Slide 7
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Another syntax for method is
+
|Another syntax for '''method''' is
  
 +
The keyword '''def '''followed by the '''method name''' and an empty '''argument list'''.
  
The keyword '''def '''followed by the '''method name''' and an empty argument list
 
  
 +
The '''ruby code''' section that represents the body of the '''method''' .
  
The '''ruby code''' section represents the body of the
+
And the word end that marks  end of '''method'''
  
'''method''' .'' ''
+
This is called as '''method without arguments'''.
 
+
 
+
The word end marks end of method
+
 
+
 
+
This is called as method without arguments.
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the editor – gedit.
+
| Switch to the editor – gedit.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let's see how to use a method.
+
|Let's see how to use a '''method'''.
 
+
  
 
I have already typed a program in the '''gedit''' editor.  
 
I have already typed a program in the '''gedit''' editor.  
Line 134: Line 110:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Point to the filename on the Title bar.
+
| Point to the filename on the Title bar.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Please note that our filename is '''method-without-argument.rb'''
+
|Please note that our filename is '''method-without-argument.rb'''
  
 
+
I have saved the file inside the  '''rubyprogram''' folder.
I have saved the file inside our '''rubyprogram''' folder.
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this program we will calculate the sum of two numbers using '''method'''.
+
|In this program we will calculate the sum of two numbers using '''method'''.
 
+
 
+
 
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us go through the program.
+
|Let us go through the program.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight''' $a=5 '''
+
| Highlight''' $a=5 '''
 
+
|Here we have declared a global variable '''a.'''
 
+
 
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have declared a global variable '''a.'''
+
 
+
  
 
And we have initialized it by assigning value of '''5'''.  
 
And we have initialized it by assigning value of '''5'''.  
 
  
 
Global variable names are prefixed with a''' dollar sign ($)'''.''' '''
 
Global variable names are prefixed with a''' dollar sign ($)'''.''' '''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Global variables are accessible from anywhere in the '''Ruby''' program;
+
|Global variables are accessible from anywhere in the '''Ruby''' program;
 
+
  
 
regardless of where they are declared.
 
regardless of where they are declared.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''def add()'''
+
| Highlight '''def add()'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have declared a method '''add''' without any arguments.
+
|Here we have declared a method called '''add''' without any '''arguments'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''print"Enter the second number:"'''
+
| Highlight '''print"Enter the second number:"'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we ask the user to enter the second number.
+
|Here we ask the user to enter the second number.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''b=gets.to_i'''
+
| Highlight '''b=gets.to_i'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| User will enter the value.  
+
|User will enter the value.  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''gets'''
+
| Highlight '''gets'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''gets method''' gets user input from the console but in '''string '''format.
+
|'''gets method''' gets the  input from the console but in '''string '''format.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''to_i'''
+
| Highlight '''to_i'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| So we need to convert it into '''integer''', using '''to_i '''method.
+
|So we need to convert it into '''integer''', using '''to_i '''method.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''b'''
+
| Highlight '''b'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The converted value is stored in the variable '''b .'''
+
| The converted value is then stored in the variable '''b .'''
 
+
  
 
'''b '''is a local variable.
 
'''b '''is a local variable.
 
  
 
It is available only to the '''method''' inside which it is declared.
 
It is available only to the '''method''' inside which it is declared.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''sum=$a+b'''
+
| Highlight '''sum=$a+b'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we add the values of global variable '''a '''and variable '''b.'''
+
|Here we add the values of global variable '''a '''and variable '''b.'''
  
 
+
The result is then stored in variable '''sum.'''
The result is stored in variable '''sum.'''
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''puts"Sum of#{ $a} & #{b} is #{sum}"'''
+
| Highlight '''puts"Sum of#{ $a} & #{b} is #{sum}"'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we print the '''sum.'''
+
|Then we print the '''sum.'''
 
+
 
+
 
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''<nowiki>#{sum}</nowiki>'''
+
| Highlight '''<nowiki>#{sum}</nowiki>'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This shows a way of inserting a variable within a string.
+
|This shows a way of inserting a variable within a string.
  
 
Here the content of '''sum''' is returned as a '''string''' and is
 
Here the content of '''sum''' is returned as a '''string''' and is
Line 224: Line 184:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''end'''
+
| Highlight '''end'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''end''' marks end of the method.
+
|'''end''' marks end of the method.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| There are two types of methods.
+
|There are two types of methods.
  
 
'''User-defined method''' - that is our '''add''' method.
 
'''User-defined method''' - that is our '''add''' method.
Line 236: Line 196:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''add()'''
+
| Highlight '''add()'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we call the '''add '''method.
+
|Here we call the '''add '''method.
  
The addition operation will be performed and the result will be  
+
The addition operation will be performed and the result will be printed.  
 
+
printed.  
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on '''Save.'''
+
| Click on '''Save.'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us click on '''Save. '''
+
|Now let us click on '''Save. '''
  
 
This program will be saved in''' rubyprogram''' folder as mentioned earlier.
 
This program will be saved in''' rubyprogram''' folder as mentioned earlier.
 
  
 
Now let us execute the program.
 
Now let us execute the program.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Ctrl+Alt+t'''
+
| Press '''Ctrl+Alt+t'''
 
+
  
 
'''Switch to Terminal'''
 
'''Switch to Terminal'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Open the terminal by pressing the '''Ctrl, Alt and T '''keys simultaneously.  
+
|Open the terminal by pressing the '''Ctrl, Alt and T '''keys simultaneously.  
 
+
  
A terminal window appears on the screen.
+
A '''terminal''' window appears on your screen.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To execute the program, we need to go to the subdirectory '''rubyprogram'''.
+
|To execute the program, we need to go to the subdirectory '''rubyprogram'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type '''cd Desktop/rubyprogram'''<nowiki><< Press </nowiki>'''Enter'''
+
| Type '''cd Desktop/rubyprogram'''<nowiki><< Press </nowiki>'''Enter'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| So let's type '''cd'' '''''space'' '''''Desktop/rubyprogram'' '''''and press'' '''''Enter.'''
+
|So let's type '''cd'' '''''space'' '''''Desktop/rubyprogram'' '''''and press'' '''''Enter.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type''' ruby method-without-argument.rb'''<nowiki> <<Press </nowiki>'''Enter'''
+
|Now  Type''' ruby method-without-argument.rb'''<nowiki> <<Press </nowiki>'''Enter'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type '''ruby '''space '''method-without-argument.rb''' and press '''Enter'''
+
|Type '''ruby '''space '''method-without-argument.rb''' and press '''Enter'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Enter the second number '''is displayed.
+
|'''Enter the second number '''is displayed.
  
 
I will enter value as '''4.'''
 
I will enter value as '''4.'''
  
type 4 and press enter
+
Type '''4''' and press '''Enter'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''Sum of two numbers 5 and 4 is 9'''
+
| Highlight '''Sum of two numbers 5 and 4 is 9'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We get the output as -  
+
|We get the output as -  
 
+
  
 
'''Sum of two numbers 5 and 4 is 9'''
 
'''Sum of two numbers 5 and 4 is 9'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Switch back to editor'''
+
| '''Switch back to editor'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us see an example of '''method''' with arguments.
+
|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.
+
 
+
 
+
  
 +
I have already typed this program in the '''gedit''' editor, let me open it.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Point to the filename in the '''Title bar.'''
+
| Point to the filename in the '''Title bar.'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Please note that our filename is '''method-with-argument.rb'''
+
|Please note that our filename is '''method-with-argument.rb'''
 
+
  
 
I have saved this file also inside the '''rubyprogram''' folder.
 
I have saved this file also inside the '''rubyprogram''' folder.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us go through the program.
+
|Let us go through the program.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''def add(a ,b)'''
+
| Highlight '''def add(a ,b)'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have declared a '''method''' called '''add.'''  
+
|Here we have declared a '''method''' called '''add.'''  
  
 
'''a,b''' are the arguments of the '''method add. '''
 
'''a,b''' are the arguments of the '''method add. '''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight''' return a+b '''
+
| Highlight''' return a+b '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here the values of '''a''' and '''b''' are added.
+
|Here the values of '''a''' and '''b''' are added.
  
 
And the sum is returned to the '''method '''call.
 
And the sum is returned to the '''method '''call.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''end'''
+
| Highlight '''end'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''end''' marks the end of method.
+
|'''end''' marks the end of method.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''puts"Enter the values of a and b"'''
+
| Highlight '''puts"Enter the values of a and b"'''
  
 
'''a=gets.to_i'''
 
'''a=gets.to_i'''
  
 
'''b=gets.to_i'''
 
'''b=gets.to_i'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we are asking the user for input.  
+
|Here we are asking the user for input.  
 
+
  
 
User will enter the values of a and b.  
 
User will enter the values of a and b.  
 
  
 
The values will be stored in variable '''a''' and variable '''b, '''respectively.
 
The values will be stored in variable '''a''' and variable '''b, '''respectively.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''c=add(a,b)'''
+
| Highlight '''c=add(a,b)'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we call the '''method''' '''add'''
+
|Here we call the '''add method'''  
  
 
Then we pass the arguments as '''a''' and '''b.'''
 
Then we pass the arguments as '''a''' and '''b.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''c'''
+
| Highlight '''c'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The value returned by the '''method add''' ,''' '''after performing the addition operation will be stored in '''c.'''
+
|The value returned by the '''method add''' ,after performing the addition operation will be stored in '''c.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''puts"Sum of two number #{a} and #{b} is #{c}"'''
+
| Highlight '''puts"Sum of two number #{a} and #{b} is #{c}"'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we print the sum which is store in '''c'''.
+
|Here we print the sum which is store in '''c'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch back to terminal
+
| Switch back to '''terminal'''
 
+
  
 
Type '''clear'''<nowiki><<Press Enter </nowiki>
 
Type '''clear'''<nowiki><<Press Enter </nowiki>
  
 +
|Let's execute this code.
  
 +
Go to the '''terminal'''.
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Lets execute this code.
+
Let us first clear the '''terminal'''
  
Go to the terminal.
+
Type''' clear''' and press '''Enter'''
 
+
Let us first clear the terminal
+
 
+
Type''' clear''' and press enter
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Point to the command prompt to show subdirectory '''rubyprogram'''
+
| Point to the command prompt to show subdirectory '''rubyprogram'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We are already in the subdirectory '''rubyprogram.'''
+
|We are already in the subdirectory '''rubyprogram.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Replace
+
| Replace
  
 
'''ruby method-with-argument.rb '''<nowiki><< Press</nowiki>''' Enter'''
 
'''ruby method-with-argument.rb '''<nowiki><< Press</nowiki>''' Enter'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now, press the up arrow key twice to get the previous command.
+
|Now, press the up arrow key twice to get the previous command.
  
 
Replace '''method-without-arguments.rb''' with '''method-with-arguments.rb '''
 
Replace '''method-without-arguments.rb''' with '''method-with-arguments.rb '''
  
 
+
And Press''' Enter'''
Press''' Enter'''
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Enter the values of a and b '''is displayed.
+
|'''Enter the values of a and b '''is displayed.
  
 
I will enter''' 8 '''and''' 9.'''
 
I will enter''' 8 '''and''' 9.'''
  
Type 8 and press Enter
+
Type 8 and press '''Enter'''
  
Type 9 and press Enter
+
Type 9 and press '''Enter'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''Sum of two numbers 8 and 9 is 17'''
+
| Highlight '''Sum of two numbers 8 and 9 is 17'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We get the output as
+
|We get the output as
  
 
'''Sum of two numbers 8 and 9 is 17.'''
 
'''Sum of two numbers 8 and 9 is 17.'''
 
  
 
<nowiki><<pause>></nowiki>
 
<nowiki><<pause>></nowiki>
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now I will show you one important feature of '''Ruby''' '''method.'''
+
|Now I will show you one important feature of '''Ruby''' '''method.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch back to the text editor
+
| Switch back to the text editor
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let's go back to the program in the text editor.
+
|Let's go back to the program in the text editor.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Delete the keyword r'''eturn.'''
+
|Delete the keyword r'''eturn.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Click on Save'''
+
| '''Click on Save'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on '''Save''' button.
+
|Now click on '''Save''' button.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch back to terminal
+
| Switch back to '''terminal'''
 +
|Let's execute the code.
  
 
+
Go to the '''terminal'''.
 
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let's execute the code.
+
 
+
 
+
Go to the terminal.
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Replace
+
| Replace
  
 
'''ruby method-with-argument.rb '''<nowiki><< Press</nowiki>''' Enter'''
 
'''ruby method-with-argument.rb '''<nowiki><< Press</nowiki>''' Enter'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Press the up arrow key to get the previous command.
+
|Press the up arrow key to get the previous command and
 
+
  
 
Press''' Enter.'''
 
Press''' Enter.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Enter the values of a and b '''is displayed.  
+
|'''Enter the values of a and b '''is displayed.  
  
 
I will enter''' 10 '''and''' 15.'''
 
I will enter''' 10 '''and''' 15.'''
  
Type 10 and press Enter
+
Type 10 and press '''Enter'''
  
Type 15 and press Enter
+
Type 15 and press '''Enter'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''Sum of two numbers 10 and 15 is 25'''
+
| Highlight '''Sum of two numbers 10 and 15 is 25'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We get the output as
+
|We get the output as
  
 
'''Sum of two numbers10 and 15 is 25. '''
 
'''Sum of two numbers10 and 15 is 25. '''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We can see that the program is executed without any error, even after deleting the keyword '''return.'''
+
|We can see that the program is executed without any error, even after deleting the keyword '''return.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Switch back to the program'''
+
| '''Switch back to the program'''
  
 
'''Highlight a+b '''
 
'''Highlight a+b '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is because '''Ruby '''automatically returns the value calculated in the '''method.'''
+
|This is because '''Ruby '''automatically returns the value calculated in the '''method.'''
  
 
The keyword '''return''' in '''method''' is optional in '''Ruby.'''
 
The keyword '''return''' in '''method''' is optional in '''Ruby.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| <nowiki><<Pause>></nowiki>
+
|<nowiki><<Pause>></nowiki>
  
 
This brings us to the end of this Spoken Tutorial.
 
This brings us to the end of this Spoken Tutorial.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 11
+
| Slide 11
 
+
  
 
Summary
 
Summary
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us switch back to slides
+
|Let us switch back to slides
  
 
Let's summarize
 
Let's summarize
 
  
 
In this tutorial we have learnt
 
In this tutorial we have learnt
  
 
* About '''Methods'''
 
* About '''Methods'''
* Syntax  
+
* Syntax for
 
* '''Method''' without arguments
 
* '''Method''' without arguments
* '''Method''' with arguments
+
* and '''Method''' with arguments
 
* Returning value from '''method'''<br/>  
 
* Returning value from '''method'''<br/>  
 
 
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 13
+
| Slide 13
  
 
Assignment
 
Assignment
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As an assignment
+
|As an assignment
  
 
Write a program  
 
Write a program  
  
 
* to calculate area of a square  
 
* to calculate area of a square  
* by using '''method '''and
+
* by using '''method '''and  
 
* by getting the input from user  
 
* by getting the input from user  
 
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 14
+
| Slide 14
  
 
About the Spoken Tutorial Project
 
About the Spoken Tutorial Project
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Watch the video available at the following link.
+
|Watch the video available at the following link.
 
+
  
 
It summarises the Spoken Tutorial project.
 
It summarises the Spoken Tutorial project.
Line 521: Line 451:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 15
+
| Slide 15
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team :
+
|The Spoken Tutorial Project Team :
  
  
Line 532: Line 462:
  
 
[mailto:contact@spoken-tutorial.org contact@spoken-tutorial.org]
 
[mailto:contact@spoken-tutorial.org contact@spoken-tutorial.org]
 
 
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 16
+
| Slide 16
  
  
 
Acknowledgement
 
Acknowledgement
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is a part of the Talk to a Teacher project.
+
|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.
 
It is supported by the National Mission on Education through ICT, MHRD, Government of India.

Latest revision as of 19:42, 18 August 2013

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
  • We will see 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 for 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 at 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 that represents the body of the method .

And the word end that 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 the 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 called 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 the input from the console but in a string format.
Highlight to_i So we need to convert it into integer, using to_i method.
Highlight b The converted value is then 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 then 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 your 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.
Now 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, 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 add method

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

Let's 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

And 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 and

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 for
  • Method without arguments
  • and 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