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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(5 intermediate revisions by 4 users not shown)
Line 2: Line 2:
  
 
{| border=1
 
{| border=1
|| ''Time'''
+
|| '''Time'''
 
|| '''Narration'''
 
|| '''Narration'''
 
  
 
|-
 
|-
|  00.01
+
|  00:01
| Welcome to the Spoken Tutorial on '''Ruby Methods'''.
+
| Welcome to the '''Spoken Tutorial''' on '''Ruby Methods'''.
  
 
|-
 
|-
| 00.05
+
| 00:05
|  In this tutorial we will learn
+
|  In this tutorial, we will learn:
  
 
|-
 
|-
| 00.07
+
| 00:07
 
| What is a '''method''' ?
 
| What is a '''method''' ?
 +
 
|-
 
|-
| 00.09
+
| 00:09
 
| Syntax for '''method''' and
 
| Syntax for '''method''' and
 +
 
|-
 
|-
| 00.11
+
| 00:11
| We will Some examples
+
| we will see some examples.
  
 
|-
 
|-
|  00.13
+
|  00:13
|  Here we are using ''Ubuntu Linux '''version 12.04 '''Ruby''' 1.9.3
+
|  Here, we are using: '''Ubuntu Linux '''version '''12.04''''''Ruby 1.9.3'''
  
 
|-
 
|-
|  00.21
+
|  00:21
| To follow this tutorial you must have knowledge of using '''Terminal '''and '''Text editor '''in '''Linux'''.  
+
| To follow this tutorial, you must have the knowledge of using '''Terminal '''and '''Text editor '''in '''Linux'''.  
  
 
|-
 
|-
|  00.28
+
|  00:28
 
|  Let us now start with an introduction to '''methods.'''
 
|  Let us now start with an introduction to '''methods.'''
  
 
|-
 
|-
| 00.31
+
| 00:31
|A '''Method''' is a self-contained program executing a specific task.  
+
|A '''method''' is a self-contained program executing a specific task.  
  
 
|-
 
|-
| 00.37
+
| 00:37
|'''Ruby method''' is very similar to functions in any other programming language.
+
|'''Ruby method''' is very similar to '''function'''s in any other programming language.
  
 
|-
 
|-
|  00.42
+
|  00:42
| | '''Method''' name should begin with a lowercase letter.
+
| '''method''' name should begin with a lowercase letter.
  
 
|-
 
|-
| 00.46
+
| 00:45
|'''Methods''' should be defined before calling them.
+
|'''method'''s should be defined before calling them.
  
 
|-
 
|-
|  00.50
+
|  00:49
|  Let us see the syntax for '''Method'''
+
|  Let us see the syntax for '''method'''.
 
+
+
  
 
|-
 
|-
| 00.52  
+
| 00:52  
|'''Methods '''are defined using the keyword '''def''' followed by the '''method''' name.
+
|'''method'''s are defined using the keyword '''"def"''', followed by the '''method''' name.
 
+
  
 
|-
 
|-
| 00.57
+
| 00:57
 
|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.  
 
  
 
|-
 
|-
| 01.03
+
| 01:02
|The '''ruby code''' section represents the body of the  '''method''' that performs the processing.'' ''
+
| '''ruby code''' section represents the body of the  '''method''' that performs the processing.
 
+
  
 
|-
 
|-
| 01.10
+
| 01:09
|The '''method''' body is enclosed by this definition on the top and the word '''end''' on the bottom
+
|The '''method''' body is enclosed by this definition on the top and the word '''"end"''' on the bottom.
  
 
|-
 
|-
| 01.16
+
| 01:16
|This is called as method with arguments.
+
|This is called as '''method''' with '''argument'''s.
  
 
|-
 
|-
|  01.20
+
|  01:19
|  Another syntax for method is
+
|  Another syntax for method is-
 
+
  
 
|-
 
|-
| 01.23
+
| 01:23
|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''',
 
+
  
 
|-
 
|-
| 01.29
+
| 01:28
|The '''ruby code''' section  that represents the body of the '''method''' .'' ''
+
| '''ruby code''' section  that represents the body of the '''method'''
  
 
|-
 
|-
| 01.32
+
| 01:32
|And the word end that marks end of method
+
|and the word "end" that marks the end of '''method'''.
  
 
|-
 
|-
| 01.36
+
| 01:36
 
|This is called as method without arguments.
 
|This is called as method without arguments.
  
 
|-
 
|-
| 01.40
+
| 01:39
|  Let's see how to use a method.
+
|  Let's see how to use a '''method'''.
  
 
|-
 
|-
| 01.43
+
| 01:42
 
|I have already typed a program in the '''gedit''' editor.  
 
|I have already typed a program in the '''gedit''' editor.  
  
 
|-
 
|-
| 01.47
+
| 01:46
 
|Let me open it.
 
|Let me open it.
  
 
|-
 
|-
|  01.48
+
|  01:48
|  Please note that our filename is '''method-without-argument.rb'''
+
|  Please note that our filename is '''method hyphen without hyphen argument dot rb'''.
  
 
|-
 
|-
|  01.55
+
|  01:55
|I have saved the file inside the  '''rubyprogram''' folder.
+
|I have saved this file inside the  '''rubyprogram''' folder.
  
 
|-
 
|-
|  01.59
+
|  01:59
|   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'''.
  
 
|-
 
|-
| 02.04
+
| 02:03
| | Let us go through the program.
+
| Let us go through the program.
  
 
|-
 
|-
|  02.06
+
|  02:05
| Here we have declared a global variable '''a.'''
+
| Here, we have declared a global variable 'a'
  
 
|-
 
|-
| 02.09
+
| 02:08
|And we have initialized it by assigning value of '''5'''.  
+
|and we have initialized it by assigning value of '''5'''.  
  
 
|-
 
|-
| 02.13
+
| 02:13
|Global variable names are prefixed with a''' dollar sign ($)'''.''' '''
+
|Global variable names are prefixed with a dollar sign ($).
  
 
|-
 
|-
|  02.17
+
|  02:17
|  Global variables are accessible from anywhere in the '''Ruby''' program; regardless of where they are declared.
+
|  Global variables are accessible from anywhere in the Ruby program, regardless of where they are declared.
  
 
|-
 
|-
| 02.25
+
| 02:25
| Here we have declared a method called '''add''' without any arguments.
+
| Here we have declared a method called '''add()''', without any arguments.
  
 
|-
 
|-
| 02.31
+
| 02:31
|  Here we ask the user to enter the second number.
+
|  Here, we ask the user to enter the second number.
  
 
|-
 
|-
| 02.36
+
| 02:35
 
|  User will enter the value.  
 
|  User will enter the value.  
  
 
|-
 
|-
|  02.38
+
|  02:38
| '''gets method''' gets  the input from the console but in a  '''string '''format.
+
| 'gets' method, gets  the input from the console but in a  '''string '''format.
  
 
|-
 
|-
|  02.44
+
|  02:44
| So we need to convert it into '''integer''', using '''to_i '''method.
+
| So, we need to convert it into '''integer''', using ''''to_i' '''method.
  
 
|-
 
|-
|  02.50
+
|  02:50
|  The converted value is  then stored in the variable '''b .''' '''b '''is a local variable.
+
|  The converted value is  then stored in the variable 'b'. 'b' is a local variable.
  
 
|-
 
|-
| 02.57
+
| 02:56
 
|It is available only to the '''method''' inside which it is declared.
 
|It is available only to the '''method''' inside which it is declared.
  
 
|-
 
|-
|  03.02
+
|  03:01
| Here we add the values of global variable '''a '''and variable '''b.'''
+
| Here, we add the values of global variable 'a' and variable 'b'.
  
 
|-
 
|-
| 03.07
+
| 03:07
|The result is  then stored in variable '''sum.'''
+
|The result is  then stored in the variable '''sum.'''
  
 
|-
 
|-
|  03.11
+
|  03:10
 
| Then we print the '''sum.'''
 
| Then we print the '''sum.'''
  
 
|-
 
|-
| 03.13
+
| 03:13
|  This shows a way of inserting a variable within a string.
+
|  This shows a way of inserting a variable within a '''string'''.
  
 
|-
 
|-
| 03.18
+
| 03:18
|Here the content of '''sum''' is returned as a '''string''' and is substituted into the outer '''string'''.
+
|Here, the content of '''sum''' is returned as a '''string''' and is substituted into the outer '''string'''.
  
 
|-
 
|-
|   03.26
+
| 03:25
|   '''end''' marks end of the method.
+
| '''end''' marks the end of the '''method'''.
  
 
|-
 
|-
| 03.29
+
| 03:28
|   There are two types of methods.
+
|There are two types of methods.
  
 
|-
 
|-
| 03.32
+
| 03:31
 
|'''User-defined method''' - that is our '''add''' method.
 
|'''User-defined method''' - that is our '''add''' method.
  
 
|-
 
|-
| 03.35
+
| 03:35
|'''Pre-defined method '''- that is '''print''', '''gets''' and '''to_i''' method.
+
|'''Pre-defined method '''- that is '''print''', '''gets''' and '''to_i''' methods.
  
 
|-
 
|-
|  03.42
+
|  03:42
|  Here we call our''add '''method.
+
|  Here, we call our '''add '''method.
  
 
|-
 
|-
| 03.46
+
| 03:45
 
|The addition operation will be performed and the result will be  printed.  
 
|The addition operation will be performed and the result will be  printed.  
  
 
|-
 
|-
| 03.50
+
| 03:50
|  Now let us click on the  '''Save. '''
+
|  Now, let us click on the  '''Save''' button.
  
 
|-
 
|-
| 03.53
+
| 03:53
|This program will be saved in''' rubyprogram''' folder as mentioned earlier.
+
|This program will be saved in the '''rubyprogram''' folder as mentioned earlier.
  
 
|-
 
|-
| 03.59
+
| 03:59
|Now let us execute the program.
+
|Now, let us '''execute''' the program.
  
 
|-
 
|-
| 04.02
+
| 04:02
|  Open the terminal by pressing the '''Ctrl, Alt and T '''keys simultaneously.  
+
|  Open the '''terminal''' by pressing the '''Ctrl, Alt''' and '''T '''keys simultaneously.  
  
 
|-
 
|-
| 04.08
+
| 04:07
|A terminal window appears on your screen.
+
|A '''terminal window''' appears on your screen.
  
 
|-
 
|-
|  04.11
+
|  04:11
|  To execute the program, we need to go to the subdirectory '''rubyprogram'''.
+
|  To execute the program, we need to go to the sub-directory '''rubyprogram'''.
  
 
|-
 
|-
|  04.17
+
|  04:16
|  So let's type '''cd'' '''''space'' '''''Desktop/rubyprogram'' '''''and press'' '''''Enter.'''
+
|  So, let's type: '''cd''' ''space'' '''Desktop/rubyprogram''' and press '''Enter.'''
  
 
|-
 
|-
|  04.26
+
|  04:26
|  Now Type '''ruby '''space '''method-without-argument.rb''' and press '''Enter'''
+
|  Now, type: '''ruby''' ''space'' '''method''' hyphen '''without''' hyphen '''argument dot rb''' and press '''Enter'''.
  
 
|-
 
|-
|  04.41
+
|  04:40
|  '''Enter the second number '''is displayed.
+
|  '''"Enter the second number:" '''is displayed.
  
 
|-
 
|-
| 04.44
+
| 04:44
|I will enter value as '''4.''' type 4 and press enter
+
|I will enter the value as '''4.''' Type 4 and press '''Enter'''.
  
 
|-
 
|-
|  04.48
+
|  04:48
|  We get the output as -  '''Sum of two numbers 5 and 4 is 9'''
+
|  We get the output as -  '''"Sum of two numbers 5 and 4 is 9"'''.
  
 
|-
 
|-
|  04.53
+
|  04:53
|  Now let us see an example of '''method''' with arguments.  
+
|  Now, let us see an example of '''method''' with '''argument'''s.  
  
 
|-
 
|-
| 04.58
+
| 04:58
 
|I have already typed this program in the '''gedit''' editor,  let me open it.
 
|I have already typed this program in the '''gedit''' editor,  let me open it.
  
 
|-
 
|-
|  05.04
+
|  05:03
|  Please note that our filename is '''method-with-argument.rb'''
+
|  Please note that our filename is '''method hyphen with hyphen argument dot rb'''.
  
 
|-
 
|-
| 05.10
+
| 05:10
 
|I have saved this file also inside the '''rubyprogram''' folder.
 
|I have saved this file also inside the '''rubyprogram''' folder.
  
 
|-
 
|-
|  05.16
+
|  05:15
 
|  Let us go through the program.
 
|  Let us go through the program.
  
 
|-
 
|-
| 05.18
+
| 05:18
|  Here we have declared a '''method''' called '''add.'''  '''a,b''' are the arguments of the '''method add. '''
+
|  Here, we have declared a '''method''' called '''add().'''  'a', 'b' are the arguments of the method 'add()'.
  
 
|-
 
|-
|  05.26
+
|  05:26
|  Here the values of '''a''' and '''b''' are added.
+
|  Here, the values of 'a' and 'b' are added
  
 
|-
 
|-
| 05.29
+
| 05:29
|And the sum is returned to the '''method '''call.
+
|and the sum is returned to the method called.
  
 
|-
 
|-
|  05.32
+
|  05:31
 
|  '''end''' marks the end of method.
 
|  '''end''' marks the end of method.
  
 
|-
 
|-
|  05.36
+
|  05:35
|  Here we are asking the user for input.  
+
|  Here, we are asking the user for input.  
  
 
|-
 
|-
| 05.40
+
| 05:38
|User will enter the values of a and b.  
+
|User will enter the values of 'a' and 'b'.  
  
 
|-
 
|-
| 05.42
+
| 05:41
|The values will be stored in variable '''a''' and variable '''b, '''respectively.
+
|The values will be stored in variables 'a' and 'b' respectively.
  
 
|-
 
|-
|  05.47
+
|  05:46
|  Here we call the  '''add''' '''method'''
+
|  Here, we call the  '''add''' '''method'''.
  
 
|-
 
|-
| 05.49
+
| 05:49
|Then we pass the arguments as '''a''' and '''b.'''
+
|Then we pass the arguments as 'a' and 'b.'
  
 
|-
 
|-
|  05.53
+
|  05:52
|  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'.
  
 
|-
 
|-
06.00
+
05:59
|  Here we print the sum which is store in '''c'''.
+
|  Here, we print the sum which is stored in 'c'.
  
 
|-
 
|-
| 06.04
+
| 06:03
| Lets execute this code. Go to the terminal.
+
| Let's '''execute''' this code. Go to the terminal.
  
 
|-
 
|-
| 06.08
+
| 06:07
|Let us first clear the terminal Type''' clear''' and press enter
+
|Let us first clear the terminalType''' clear''' and press '''Enter'''.
  
 
|-
 
|-
| 06.14
+
| 06:14
| We are already in the subdirectory '''rubyprogram.'''
+
| We are already in the sub-directory '''rubyprogram.'''
  
 
|-
 
|-
| 06.17
+
| 06:17
|  Now, press the up arrow key twice to get the previous command.
+
|  Now, press the up-arrow key twice to get the previous command.
  
 
|-
 
|-
| 06.22
+
| 06:22
|Replace '''method-without-arguments.rb''' with '''method-with-arguments.rb '''
+
|Replace '''method hyphen without hyphen arguments dot rb''' with '''method hyphen with hyphen arguments dot rb'''
  
 
|-
 
|-
| 06.33
+
| 06:32
| And Press''' Enter'''
+
| and press''' Enter'''.
  
 
|-
 
|-
| 06.35
+
| 06:35
|  '''Enter the values of a and b '''is displayed.
+
|  '''"Enter the values of a and b" '''is displayed.
  
 
|-
 
|-
| 06.39
+
| 06:38
 
|I will enter''' 8 '''and''' 9.'''
 
|I will enter''' 8 '''and''' 9.'''
  
 
|-
 
|-
| 06.41
+
| 06:41
|Type 8 and press Enter
+
|Type 8 and press '''Enter'''.
  
 
|-
 
|-
| 06.43
+
| 06:43
|Type 9 and press Enter
+
|Type 9 and press '''Enter'''.
  
 
|-
 
|-
|  06.46
+
|  06:46
| We get the output as
+
| We get the output as ''''"Sum of two numbers 8 and 9 is 17".'''
  
 
|-
 
|-
| 06.47
+
| 06:52
|'''Sum of two numbers 8 and 9 is 17.'''
+
|Now, I will show you one important feature of Ruby '''method.'''
 
+
 
+
|-
+
| 06.52
+
|Now I will show you one important feature of '''Ruby''' '''method.'''
+
  
 
|-
 
|-
| 06.57
+
| 06:56
 
| Let's go back to the program in the text editor.
 
| Let's go back to the program in the text editor.
  
 
|-
 
|-
|07.00
+
|06:59
 
|Delete the keyword '''return.'''
 
|Delete the keyword '''return.'''
  
 
|-
 
|-
| 07.03
+
| 07:02
|Now click on '''Save''' button.
+
|Now, click on '''Save''' button.
  
 
|-
 
|-
| 07.05
+
| 07:05
| Let's execute the code.Go to the terminal.
+
| Let's execute the code. Go to the terminal.
  
 
|-
 
|-
|07.09
+
|07:09
| Press the up arrow key to get the previous command and  Press''' Enter.'''
+
| Press the up-arrow key to get the previous command and  press''' Enter.'''
  
 
|-
 
|-
| 07.15
+
| 07:14
|''Enter the values of a and b '''is displayed.  
+
|'''"Enter the values of a and b" '''is displayed.  
  
 
|-
 
|-
| 07.18
+
| 07:18
 
|I will enter''' 10 '''and''' 15.'''
 
|I will enter''' 10 '''and''' 15.'''
  
 
|-
 
|-
| 07.22
+
| 07:21
|Type 10 and press Enter Type 15 and press Enter
+
|Type 10, press '''Enter'''. Type 15 and press '''Enter'''.
  
 
|-
 
|-
|  07.28
+
|  07:27
 
|  We get the output as
 
|  We get the output as
  
 
|-
 
|-
| 07.29
+
| 07:29
|'''Sum of two numbers10 and 15 is 25. '''
+
|'''"Sum of two numbers 10 and 15 is 25". '''
  
 
|-
 
|-
|  07.33
+
|  07:33
|  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.'''
  
 
|-
 
|-
| 07.40
+
| 07:40
|  This is because '''Ruby '''automatically returns the value calculated in the '''method.'''
+
|  This is because Ruby automatically returns the value calculated in the '''method.'''
  
 
|-
 
|-
| 07.46
+
| 07:46
|The keyword '''return''' in '''method''' is optional in '''Ruby.'''
+
|The keyword '''return''' in the '''method''' is optional in Ruby.
  
 
|-
 
|-
| 07.50
+
| 07:50
 
|  This brings us to the end of this Spoken Tutorial.
 
|  This brings us to the end of this Spoken Tutorial.
  
 
|-
 
|-
| 07.53
+
| 07:53
| Let us switch back to slides
+
| Let us switch back to our '''slide'''.
  
 
|-
 
|-
| 07.56
+
| 07:55
|Let's summarize
+
|Let us summarize.
  
 
|-
 
|-
| 07.58
+
| 07:57
|In this tutorial we have learnt
+
|In this tutorial, we have learnt:
  
 
|-
 
|-
| 08.00
+
| 07:59
|About '''Methods'''
+
| About '''methods'''
 +
 
 
|-
 
|-
| 08.01
+
| 08:01
| Syntax  for '''Method''' without arguments
+
| Syntax  for '''method''' without arguments
|-
+
 
| 08.04
+
|And  '''Method''' with arguments
+
 
|-
 
|-
| 08.06
+
| 08:04
| Returning value from '''method'''  
+
| And '''method''' with arguments
  
 
|-
 
|-
| 08.09
+
| 08:06
| As an assignment
+
| Returning value from a '''method'''.
  
 
|-
 
|-
| 08.10
+
| 08:08
|Write a program,to calculate area of a square
+
| As an assignment,
  
 
 
|-
 
|-
| 08.13
+
| 08:10
| by using '''method '''and
+
|write a program to calculate area of a square
 +
 
|-
 
|-
| 08.15
+
| 08:13
| by getting the input from user  
+
| by using '''method '''and  by getting the input from the user.
  
 
|-
 
|-
|  08.17
+
|  08:17
 
|  Watch the video available at the following link.
 
|  Watch the video available at the following link.
  
 
|-
 
|-
| 08.20
+
| 08:20
|It summarises the Spoken Tutorial project.
+
|It summarizes the Spoken Tutorial project.
  
 
|-
 
|-
| 08.24
+
| 08:23
 
|If you do not have good bandwidth, you can download and  watch it.  
 
|If you do not have good bandwidth, you can download and  watch it.  
  
 
|-
 
|-
|  08.29
+
|  08:28
|  The Spoken Tutorial Project Team :
+
|  The Spoken Tutorial project team:
 
+
  
 
|-   
 
|-   
| 08.31
+
| 08:30
|Conducts workshops using spoken tutorials  
+
| Conducts workshops using spoken tutorials.
  
 
|-
 
|-
| 08.33
+
| 08:33
|Gives certificates to those who pass an online test  
+
| Gives certificates to those who pass an online test.
  
 
|-
 
|-
| 08.36
+
| 08:36
|For more details, please write to
+
|For more details, please write to: contact@spoken-tutorial.org
 
+
[mailto:contact@spoken-tutorial.org contact@spoken-tutorial.org]
+
  
 
|-
 
|-
|  08.45
+
|  08:44
|  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.
  
 
|-
 
|-
| 08.49
+
| 08:49
 
|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.
  
 
|-
 
|-
| 08.55
+
| 08:55
|More information on this Mission is available at:  spoken-tutorial.org/NMEICT-Intro.
+
|More information on this mission is available at the below link.
  
 
|-
 
|-
| 09.00
+
| 09:00
|This is Afrin Pinjari from IIT Bombay, signing off.
+
|This is Afrin Pinjari from '''IIT Bombay''', signing off.
  
 
|-
 
|-
| 09.05
+
| 09:04
 
|Thank you for watching.
 
|Thank you for watching.
  
 
|}
 
|}

Latest revision as of 12:06, 10 March 2017


Time Narration
00:01 Welcome to the Spoken Tutorial on Ruby Methods.
00:05 In this tutorial, we will learn:
00:07 What is a method ?
00:09 Syntax for method and
00:11 we will see some examples.
00:13 Here, we are using: Ubuntu Linux version 12.04, Ruby 1.9.3
00:21 To follow this tutorial, you must have the knowledge of using Terminal and Text editor in Linux.
00:28 Let us now start with an introduction to methods.
00:31 A method is a self-contained program executing a specific task.
00:37 Ruby method is very similar to functions in any other programming language.
00:42 method name should begin with a lowercase letter.
00:45 methods should be defined before calling them.
00:49 Let us see the syntax for method.
00:52 methods are defined using the keyword "def", followed by the method name.
00:57 The arguments specify values that are passed to the method, to be processed.
01:02 ruby code section represents the body of the method that performs the processing.
01:09 The method body is enclosed by this definition on the top and the word "end" on the bottom.
01:16 This is called as method with arguments.
01:19 Another syntax for method is-
01:23 the keyword "def" followed by the method name and an empty argument list,
01:28 ruby code section that represents the body of the method
01:32 and the word "end" that marks the end of method.
01:36 This is called as method without arguments.
01:39 Let's see how to use a method.
01:42 I have already typed a program in the gedit editor.
01:46 Let me open it.
01:48 Please note that our filename is method hyphen without hyphen argument dot rb.
01:55 I have saved this file inside the rubyprogram folder.
01:59 In this program, we will calculate the sum of two numbers using method.
02:03 Let us go through the program.
02:05 Here, we have declared a global variable 'a'
02:08 and we have initialized it by assigning value of 5.
02:13 Global variable names are prefixed with a dollar sign ($).
02:17 Global variables are accessible from anywhere in the Ruby program, regardless of where they are declared.
02:25 Here we have declared a method called add(), without any arguments.
02:31 Here, we ask the user to enter the second number.
02:35 User will enter the value.
02:38 'gets' method, gets the input from the console but in a string format.
02:44 So, we need to convert it into integer, using 'to_i' method.
02:50 The converted value is then stored in the variable 'b'. 'b' is a local variable.
02:56 It is available only to the method inside which it is declared.
03:01 Here, we add the values of global variable 'a' and variable 'b'.
03:07 The result is then stored in the variable sum.
03:10 Then we print the sum.
03:13 This shows a way of inserting a variable within a string.
03:18 Here, the content of sum is returned as a string and is substituted into the outer string.
03:25 end marks the end of the method.
03:28 There are two types of methods.
03:31 User-defined method - that is our add method.
03:35 Pre-defined method - that is print, gets and to_i methods.
03:42 Here, we call our add method.
03:45 The addition operation will be performed and the result will be printed.
03:50 Now, let us click on the Save button.
03:53 This program will be saved in the rubyprogram folder as mentioned earlier.
03:59 Now, let us execute the program.
04:02 Open the terminal by pressing the Ctrl, Alt and T keys simultaneously.
04:07 A terminal window appears on your screen.
04:11 To execute the program, we need to go to the sub-directory rubyprogram.
04:16 So, let's type: cd space Desktop/rubyprogram and press Enter.
04:26 Now, type: ruby space method hyphen without hyphen argument dot rb and press Enter.
04:40 "Enter the second number:" is displayed.
04:44 I will enter the value as 4. Type 4 and press Enter.
04:48 We get the output as - "Sum of two numbers 5 and 4 is 9".
04:53 Now, let us see an example of method with arguments.
04:58 I have already typed this program in the gedit editor, let me open it.
05:03 Please note that our filename is method hyphen with hyphen argument dot rb.
05:10 I have saved this file also inside the rubyprogram folder.
05:15 Let us go through the program.
05:18 Here, we have declared a method called add(). 'a', 'b' are the arguments of the method 'add()'.
05:26 Here, the values of 'a' and 'b' are added
05:29 and the sum is returned to the method called.
05:31 end marks the end of method.
05:35 Here, we are asking the user for input.
05:38 User will enter the values of 'a' and 'b'.
05:41 The values will be stored in variables 'a' and 'b' respectively.
05:46 Here, we call the add method.
05:49 Then we pass the arguments as 'a' and 'b.'
05:52 The value returned by the method 'add()', after performing the addition operation, will be stored in 'c'.
05:59 Here, we print the sum which is stored in 'c'.
06:03 Let's execute this code. Go to the terminal.
06:07 Let us first clear the terminal. Type clear and press Enter.
06:14 We are already in the sub-directory rubyprogram.
06:17 Now, press the up-arrow key twice to get the previous command.
06:22 Replace method hyphen without hyphen arguments dot rb with method hyphen with hyphen arguments dot rb
06:32 and press Enter.
06:35 "Enter the values of a and b" is displayed.
06:38 I will enter 8 and 9.
06:41 Type 8 and press Enter.
06:43 Type 9 and press Enter.
06:46 We get the output as '"Sum of two numbers 8 and 9 is 17".
06:52 Now, I will show you one important feature of Ruby method.
06:56 Let's go back to the program in the text editor.
06:59 Delete the keyword return.
07:02 Now, click on Save button.
07:05 Let's execute the code. Go to the terminal.
07:09 Press the up-arrow key to get the previous command and press Enter.
07:14 "Enter the values of a and b" is displayed.
07:18 I will enter 10 and 15.
07:21 Type 10, press Enter. Type 15 and press Enter.
07:27 We get the output as
07:29 "Sum of two numbers 10 and 15 is 25".
07:33 We can see that the program is executed without any error, even after deleting the keyword return.
07:40 This is because Ruby automatically returns the value calculated in the method.
07:46 The keyword return in the method is optional in Ruby.
07:50 This brings us to the end of this Spoken Tutorial.
07:53 Let us switch back to our slide.
07:55 Let us summarize.
07:57 In this tutorial, we have learnt:
07:59 About methods
08:01 Syntax for method without arguments
08:04 And method with arguments
08:06 Returning value from a method.
08:08 As an assignment,
08:10 write a program to calculate area of a square
08:13 by using method and by getting the input from the user.
08:17 Watch the video available at the following link.
08:20 It summarizes the Spoken Tutorial project.
08:23 If you do not have good bandwidth, you can download and watch it.
08:28 The Spoken Tutorial project team:
08:30 Conducts workshops using spoken tutorials.
08:33 Gives certificates to those who pass an online test.
08:36 For more details, please write to: contact@spoken-tutorial.org
08:44 Spoken Tutorial project is a part of the Talk to a Teacher project.
08:49 It is supported by the National Mission on Education through ICT, MHRD, Government of India.
08:55 More information on this mission is available at the below link.
09:00 This is Afrin Pinjari from IIT Bombay, signing off.
09:04 Thank you for watching.

Contributors and Content Editors

PoojaMoolya, Pratik kamble, Priyacst, Sandhya.np14, Sneha