Difference between revisions of "Ruby/C3/Object-Oriented-Concept-in-Ruby/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ' {| border=1 || ''Time''' || '''Narration''' |- | 00.01 | Welcome to this spoken tutorial on ''' Object Oriented Concept''' in '''Ruby'''. |- | 00.06 | In this tutorial we …')
 
 
(14 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 this spoken tutorial on ''' Object Oriented Concept''' in '''Ruby'''.  
+
| Welcome to this '''spoken tutorial''' on '''Object Oriented Concept''' in '''Ruby'''.  
 
+
 
 
|-
 
|-
| 00.06
+
| 00:06
| In this tutorial we will learn to use-
+
| In this tutorial, we will learn to
  
 
|-
 
|-
| 00.08
+
| 00:08
| '''classes'''
+
| use '''classes''',  creating '''objects'''  
  
 
|-
 
|-
| 00.09
+
| 00:10
|creating '''objects'''  
+
| different ways of defining '''methods''' in '''Ruby'''.
  
 
|-
 
|-
| 00.10
+
| 00:13
|different ways of defining '''methods''' in '''Ruby'''
+
|Here we are using: '''Ubuntu''' version '''12.04'''  
  
 
|-
 
|-
| 00.13
+
| 00:16
|Here we are using
+
| ''' Ruby 1.9.3'''
  
 
|-
 
|-
| 00.14
+
| 00:19
|  ''' Ubuntu''' version 12.04
+
|To follow this tutorial, you must have knowledge of ''' Linux command'''s, ''' Terminal''' and '''Text-editor.'''
 
+
|-
+
| 00.16
+
|''' Ruby '''1.9.3
+
 
+
|-
+
| 00.19
+
|To follow this tutorial, you must have knowledge of ''' Linux''' commands, ''' Terminal''' and '''Text-editor.'''
+
 
    
 
    
 
|-
 
|-
|  00.24
+
|  00:24
 
| If not, for relevant tutorials, please visit our website.  
 
| If not, for relevant tutorials, please visit our website.  
  
 
|-
 
|-
|  00.28
+
|  00:28
 
| Before we begin, recall that we had created a ''' “ttt” ''' directory earlier.  
 
| Before we begin, recall that we had created a ''' “ttt” ''' directory earlier.  
  
 
|-
 
|-
| 00.33
+
| 00:33
 
|Let's go to that directory.  
 
|Let's go to that directory.  
  
 
|-
 
|-
| 00.35
+
| 00:35
 
|To ''' ruby hyphen tutorial''' and ''' classes''' directory.  
 
|To ''' ruby hyphen tutorial''' and ''' classes''' directory.  
  
 
|-
 
|-
| 00.41
+
| 00:41
 
|''' Ruby''' is an ''' object oriented''' language.  
 
|''' Ruby''' is an ''' object oriented''' language.  
  
 
|-
 
|-
| 00.44
+
| 00:44
| Everything in '''Ruby''' is an object; from a value to a '''string''' or number.  
+
| Everything in Ruby is an object; from a value to a '''string''' or number.  
 
+
  
 
|-
 
|-
| 00.49
+
| 00:49
|A '''class''' is a collection of related '''data''' and '''functions.''  
+
|A '''class''' is a collection of related '''data''' and '''functions.'''  
  
 
|-
 
|-
|  00.53
+
|  00:53
 
|It can serve to keep information organized.  
 
|It can serve to keep information organized.  
  
 
|-
 
|-
| 00.56
+
| 00:56
 
|An '''object''' is an ''' instantiation''' of a '''class.'''  
 
|An '''object''' is an ''' instantiation''' of a '''class.'''  
 
   
 
   
 
|-
 
|-
| 01.00
+
| 01:00
 
| A '''class''' definition begins with the keyword '''class.'''  
 
| A '''class''' definition begins with the keyword '''class.'''  
  
 
|-
 
|-
| 01.05
+
| 01:05
 
| It is followed by the name of the '''class.'''  
 
| It is followed by the name of the '''class.'''  
 
    
 
    
 
|-
 
|-
| 01.08
+
| 01:08
 
| It is delimited with an ''' “end”.'''  
 
| It is delimited with an ''' “end”.'''  
 
  
 
|-
 
|-
| 01.11
+
| 01:11
 
|Let us look at an example of a ''' class.'''
 
|Let us look at an example of a ''' class.'''
 
   
 
   
 
|-
 
|-
| 01.14
+
| 01:14
 
|''' class Product'''
 
|''' class Product'''
  
 
|-
 
|-
| 01.16
+
| 01:16
|''' ruby code'''  
+
|''' ruby code''', '''end'''
+
|-
+
| 01.17
+
|'''end'''
+
  
 
|-
 
|-
|  01.20
+
|  01:20
 
| The name of the '''class''' must begin with a capital letter.  
 
| The name of the '''class''' must begin with a capital letter.  
  
 
|-
 
|-
|  01.24
+
|  01:24
 
|Names that contain more than one word should be camelcased.  
 
|Names that contain more than one word should be camelcased.  
  
 
|-
 
|-
|  01.28
+
|  01:28
 
| For example,  
 
| For example,  
 
   
 
   
 
|-
 
|-
| 01.30
+
| 01:30
 
| ''' UserInformation'''
 
| ''' UserInformation'''
  
 
|-
 
|-
|  01.32
+
|  01:32
 
| '''ProductInformation'''
 
| '''ProductInformation'''
  
 
|-
 
|-
| 01.34
+
| 01:34
 
|The subsequent file names will have underscore separating the words:  
 
|The subsequent file names will have underscore separating the words:  
 
  
 
|-
 
|-
| 01.37
+
| 01:37
 
|''' user underscore information'''  
 
|''' user underscore information'''  
  
 
|-
 
|-
| 01.40
+
| 01:40
 
|''' product underscore information'''  
 
|''' product underscore information'''  
  
 
|-
 
|-
|  01.45
+
|  01:45
| Create a new file in ''' gedit''' as shown in the basic level '''Ruby''' tutorials.  
+
| Create a new file in ''' gedit''' as shown in the basic level '''Ruby tutorials'''.  
  
 
|-
 
|-
| 01.48
+
| 01:48
 
| Name it ''' class_definition.rb'''
 
| Name it ''' class_definition.rb'''
  
 
|-
 
|-
| 01.52
+
| 01:52
 
| I have a working example of the implementation of '''classes.'''
 
| I have a working example of the implementation of '''classes.'''
  
 
|-
 
|-
|  01.57
+
|  01:57
|You can pause the tutorial, and type the code as we go through it.  
+
|You can pause the tutorial and type the code as we go through it.  
  
 
|-
 
|-
|  02.02
+
|  02:02
 
| I have defined a ''' class''' named ''' Order''' in this example.  
 
| I have defined a ''' class''' named ''' Order''' in this example.  
  
 
|-
 
|-
|  02.05
+
|  02:05
| Now let us make the ''' class''' useful by adding some '''variables.'''  
+
| Now, let us make the ''' class''' useful by adding some '''variables.'''  
  
 
|-
 
|-
| 02.11
+
| 02:11
|Then I have defined an ''' instance variable “myinstance”.'''
+
|Then I have defined an '''instance variable “myinstance”.'''
  
 
|-
 
|-
|  02.15
+
|  02:15
 
|And I have assigned a value to it.  
 
|And I have assigned a value to it.  
  
 
|-
 
|-
|  02.18
+
|  02:18
|I have also defined a ''' class variable “myclassvar” '''
+
|I have also defined a ''' class variable “myclassvar” '''.
  
 
|-
 
|-
|  02.21
+
|  02:21
 
|And I have assigned a value to it.  
 
|And I have assigned a value to it.  
  
 
|-
 
|-
| 02.24
+
| 02:24
|Now let us add some code that will make use of this class.  
+
|Now, let us add some code that will make use of this '''class'''.  
  
 
|-
 
|-
|  02.30
+
|  02:30
| Type ''' puts Order dot instance underscore variables.'''
+
| Type: '''puts Order dot instance underscore variables'''
 
+
  
 
|-
 
|-
| 02.36
+
| 02:36
 
| Before this line, add '''puts''' some characters '''slash n''' for a new line.  
 
| Before this line, add '''puts''' some characters '''slash n''' for a new line.  
  
 
|-
 
|-
| 02.43
+
| 02:43
| Let us copy and paste that and add it just below the line we added and save it.  
+
| Let us '''copy''' and '''paste''' that and add it just below the line we added and '''save''' it.  
  
 
|-
 
|-
| 02.51
+
| 02:51
| Now let us execute this code
+
| Now, let us '''execute''' this code.
  
 
|-
 
|-
| 02.53
+
| 02:53
|Switch to the terminal and type  
+
|Switch to the '''terminal''' and type:
  
 
|-
 
|-
|  02.56
+
|  02:56
 
| ''' ruby space class underscore definition dot rb'''  
 
| ''' ruby space class underscore definition dot rb'''  
 
   
 
   
 
|-
 
|-
| 03.02
+
| 03:02
 
|and see the output.  
 
|and see the output.  
  
 
|-
 
|-
| 03.05
+
| 03:05
 
| You will see the ''' instance variable''' you defined.
 
| You will see the ''' instance variable''' you defined.
  
 
|-
 
|-
| 03.09
+
| 03:09
|Now let us type ''' puts Order dot class underscore variables.'''  
+
|Now, let us type: ''' puts Order dot class underscore variables'''  
  
 
|-
 
|-
| 03.15
+
| 03:15
|Let us copy and paste the demarcation, just below the line and save it.  
+
|Let us '''copy''' and '''paste''' the demarcation, just below the line and '''save''' it.  
  
 
|-
 
|-
| 03.21
+
| 03:21
| Now let us switch to the '''terminal''' and execute the file like before.   
+
| Now, let us switch to the '''terminal''' and execute the file like before.   
  
 
|-
 
|-
| 03.26
+
| 03:26
 
|You will notice the ''' class variable''' you defined, also show up.  
 
|You will notice the ''' class variable''' you defined, also show up.  
  
 
|-
 
|-
|  03.32
+
|  03:32
| To now be able to write your own '''class.'''
+
| You should now be able to write your own '''class.'''
  
 
|-
 
|-
|  03.35
+
|  03:35
 
|  Next, let us look at what an '''object''' is.   
 
|  Next, let us look at what an '''object''' is.   
  
 
|-
 
|-
|  03.40
+
|  03:40
 
|An '''object''' is an '''instance''' of a '''class.'''
 
|An '''object''' is an '''instance''' of a '''class.'''
  
 
|-
 
|-
|  03.43
+
|  03:43
| Which means an '''object''' is created from a '''class.'''  
+
| Which means, an '''object''' is created from a '''class.'''  
  
 
|-
 
|-
| 03.46
+
| 03:46
 
|An '''object''' will have the properties and '''methods''' defined in the '''class.'''  
 
|An '''object''' will have the properties and '''methods''' defined in the '''class.'''  
  
 
|-
 
|-
|  03.52
+
|  03:52
| How do you declare an '''object'''
+
| How do you declare an '''object'''?
  
 
|-
 
|-
|  03.54
+
|  03:54
|We declare an '''object''' of a class using the '''new keyword.'''  
+
|We declare an '''object''' of a class using the '''"new" keyword'''.
 
+
  
 
|-
 
|-
| 03.58
+
| 03:58
|Here we are declaring an '''object''' of the '''Product class.'''  
+
|Here, we are declaring an '''object''' of the '''Product class.'''  
  
 
|-
 
|-
|  04.02
+
|  04:02
| Here an '''object''' gets created.  
+
| Here, an '''object''' gets created.  
  
 
|-
 
|-
| 04.05
+
| 04:05
 
|''' product = Product.new'''  
 
|''' product = Product.new'''  
  
 
|-
 
|-
| 04.09
+
| 04:09
 
| This process is called ''' initialization''' of an object.  
 
| This process is called ''' initialization''' of an object.  
  
 
|-
 
|-
| 04.12
+
| 04:12
|This '''object''' is of '''type''': '''Product.'''  
+
|This '''object''' is of type '''Product.'''  
 
+
  
 
|-
 
|-
| 04.16
+
| 04:16
|Now let is look at what an ''' “initialize” method''' is.  
+
|Now, let is look at what an ''' “initialize” method''' is.  
  
 
|-
 
|-
| 04.20
+
| 04:20
|An ''' initialize method''' is called at the time of '''object''' creation.  
+
|An '''initialize method''' is '''call'''ed at the time of '''object''' creation.  
 
+
  
 
|-
 
|-
|  04.26
+
|  04:26
| On calling '''new''' on an '''object''', we invoke the '''initialize method.'''  
+
| On '''call'''ing '''"new"''' on an object, we invoke the '''initialize method.'''  
  
 
|-
 
|-
|  04.31
+
|  04:31
 
|An '''initialize method''' may take a list of '''parameters.'''   
 
|An '''initialize method''' may take a list of '''parameters.'''   
  
 
|-
 
|-
|  04.37
+
|  04:37
|Like other ''' Ruby methods''', it is preceded by the ''' “def” keyword.'''
+
|Like other ''''Ruby' methods''', it is preceded by the '''“def” keyword.'''
  
 
|-
 
|-
| 04.43
+
| 04:43
 
|Let us look at an example.  
 
|Let us look at an example.  
  
 
|-
 
|-
| 04.46
+
| 04:46
|Create a new file in '''gedit''' as shown in the basic level '''Ruby''' tutorials.  
+
|Create a new file in '''gedit''' as shown in the basic level '''Ruby Tutorials'''.  
  
 
|-
 
|-
| 04.50
+
| 04:50
| And name it '''object undescore initialize dot rb '''
+
| And name it '''object underscore initialize dot rb'''.
  
 
|-
 
|-
| 04.55
+
| 04:55
 
| I have a working example of the ''' object initialization''' code.  
 
| I have a working example of the ''' object initialization''' code.  
  
 
|-
 
|-
| 05.00
+
| 05:00
| You can pause the tutorial, and type the code as we go through it.   
+
| You can pause the tutorial and type the code as we go through it.   
  
 
|-
 
|-
| 05.04
+
| 05:04
|Here I have defined a ''' class''' called ''' “Order” '''
+
|Here, I have defined a '''class''' called '''“Order” '''.
  
 
|-
 
|-
| 05.08
+
| 05:08
 
|Then I have defined the ''' initialize method''', with no '''argument.'''
 
|Then I have defined the ''' initialize method''', with no '''argument.'''
  
 
|-
 
|-
| 05.13
+
| 05:13
| I have defined a '''puts method''' to display the message '''“I have created an object”.
+
| I have defined a ''''puts' method''' to display the message: “I have created an object”.
  
 
|-
 
|-
| 05.20
+
| 05:20
 
|Next, I have defined '''Order dot new'''.
 
|Next, I have defined '''Order dot new'''.
  
 
|-
 
|-
| 05.24
+
| 05:24
 
|This will invoke the '''initialize method.'''  
 
|This will invoke the '''initialize method.'''  
  
 
|-
 
|-
| 05.27
+
| 05:27
|Switch to the '''terminal''' and type  
+
|Switch to the '''terminal''' and type:
 
   
 
   
 
|-
 
|-
| 05.31
+
| 05:31
 
|'''ruby space object underscore initialize dot rb'''  
 
|'''ruby space object underscore initialize dot rb'''  
  
 
|-
 
|-
| 05.36
+
| 05:36
 
| and see the output.  
 
| and see the output.  
  
 
|-
 
|-
|05.39
+
|05:39
|You will see the message ''' “I have created an object” ''' .  
+
|You will see the message: ''' “I have created an object”'''.  
  
 
|-
 
|-
| 05.43
+
| 05:43
|Now let us go back to '''gedit''' and add an '''argument''' to the '''method.'''
+
|Now, let us go back to '''gedit''' and add an '''argument''' to the '''method.'''
  
 
|-
 
|-
| 05.48
+
| 05:48
|Let us modify the ''' puts''' .   
+
|Let us modify the '''puts'''.   
  
 
|-
 
|-
| 05.51
+
| 05:51
 
|It should display the value of the ''' argument''' passed.  
 
|It should display the value of the ''' argument''' passed.  
  
 
|-
 
|-
|  05.55
+
|  05:55
| Next let us type  
+
| Next, let us type: '''Order dot new(“I have created an object”).'''
  
 
|-
 
|-
| 05.56
+
| 06:04
|''' Order dot new(“I have created an object”). '''
+
|Here, we have given an argument to the '''"new" method'''.
  
 
|-
 
|-
| 06.04
+
|  06:08
|Here we have given an argument to the new method.
+
| This argument gets passed on to the '''initialize method.'''  
 
+
|-
+
|  06.08
+
| This '''argument''' gets passed on to the '''initialize method.'''  
+
 
   
 
   
 
|-
 
|-
| 06.13
+
| 06:13
|Switch to the ''' terminal''' and type  
+
|Switch to the ''' terminal''' and type:
  
 
|-
 
|-
| 06.16
+
| 06:16
|''' ruby space object underscore initialize dot rb'''  
+
|'''ruby space object underscore initialize dot rb'''  
  
 
|-
 
|-
|  06.20
+
|  06:20
 
|and see the output.  
 
|and see the output.  
  
 
|-
 
|-
| 06.22
+
| 06:22
|You will see the message ''' “I have created an object” ''' printed out.  
+
|You will see the message: ''' “I have created an object” ''' printed out.  
 
   
 
   
 
|-
 
|-
| 06.29
+
| 06:29
 
| Now, you would have understood what ''' object initialization''' means.  
 
| Now, you would have understood what ''' object initialization''' means.  
  
 
|-
 
|-
|  06.33
+
|  06:33
|Recall that in '''Ruby, methods''' are the '''functions''' that a '''class''' performs.  
+
|Recall that in Ruby, '''methods''' are the '''functions''' that a '''class''' performs.  
  
 
|-
 
|-
| 06.39
+
| 06:39
|Each '''method''' in a class is defined within the ''' “def” '''  and ''' “end”''' block.  
+
|Each '''method''' in a '''class''' is defined within the '''“def”'''  and '''“end”''' block.  
 
+
  
 
|-
 
|-
| 06.45
+
| 06:45
|A multiword ''' method'''  name is separated with an '''underscore.'''
+
|A multi-word method-name is separated with an '''underscore'''.
  
 
|-
 
|-
| 06.48
+
| 06:48
 
|Some of the characters that can be appended to a '''method''' name are:   
 
|Some of the characters that can be appended to a '''method''' name are:   
  
 
|-
 
|-
| 06.54
+
| 06:54
|''' ? (question-mark) '''
+
| '?' (question-mark)  
  
 
|-
 
|-
| 06.56
+
| 06:56
|''' = (equal to) '''
+
|'=' (equal to)
  
 
|-
 
|-
| 06.58
+
| 06:58
 
|Each of the characters add some meaning to the '''method.'''  
 
|Each of the characters add some meaning to the '''method.'''  
  
 
|-
 
|-
| 07.02
+
| 07:02
 
|Let us look at some examples.  
 
|Let us look at some examples.  
 
  
 
|-
 
|-
| 07.05
+
| 07:05
|Create a new file in ''' gedit''' as shown in the basic level '''Ruby''' tutorials.  
+
|Create a new file in '''gedit''' as shown in the basic level '''Ruby tutorials'''.  
  
 
|-
 
|-
| 07.09
+
| 07:09
 
|And name it ''' class underscore methods dot rb'''.
 
|And name it ''' class underscore methods dot rb'''.
  
 
|-
 
|-
| 07.14
+
| 07:14
 
|I have a working example of ''' class methods''' code.  
 
|I have a working example of ''' class methods''' code.  
  
 
|-
 
|-
| 07.17
+
| 07:17
|You can pause the tutorial, and type the code as we go through it.   
+
|You can pause the tutorial and type the code as we go through it.   
  
 
|-
 
|-
| 07.21
+
| 07:21
|Here I have defined a class named ''' “Animal”.'''
+
|Here, I have defined a class named “Animal”.
  
 
|-
 
|-
| 07.23
+
| 07:23
|Then I have two ''' methods “breathe” ''' and ''' “walk”.'''
+
|Then I have two methods- “breathe” and “walk”.
  
 
|-
 
|-
| 07.28
+
| 07:28
|They are both defined with the ''' “def” ''' and ''' “end”''' keywords.  
+
|They are both defined with the “def” and “end” keywords.  
  
 
|-
 
|-
| 07.32
+
| 07:32
|I have then initialized the ''' object Animal.'''
+
|I have then initialized the object 'Animal'.
  
 
|-
 
|-
| 07.36
+
| 07:36
|I have assigned it to a variable called  ''' “animal”''' with lowercase ''' “a”.'''
+
|I have assigned it to a variable called  “animal” with lowercase “a”.  
  
 
|-
 
|-
| 07.40
+
| 07:40
|Then I have invoked the ''' methods “breathe” ''' and ''' “walk” ''' consequtively.  
+
|Then I have invoked the methods “breathe” and “walk” consecutively.  
  
 
|-
 
|-
| 07.48
+
| 07:48
|Now let us execute the program.  
+
|Now, let us '''execute''' the program.  
  
 
|-
 
|-
| 07.51
+
| 07:51
|Switch to the '''terminal''' and type  
+
|Switch to the '''terminal''' and type:
  
 
|-
 
|-
| 07.53
+
| 07:53
|''' ruby space class underscore methods dot rb '''
+
|'''ruby space class underscore methods dot rb'''
  
 
|-
 
|-
| 07.58
+
| 07:58
 
|and see the output.  
 
|and see the output.  
  
 
|-
 
|-
| 08.00
+
| 08:00
 
|You will notice the lines:  
 
|You will notice the lines:  
  
 
|-
 
|-
| 08.02
+
| 08:02
|'''“ I breathe” '''
+
|'''“ I breathe” ''', '''“I walk” '''
  
 
|-
 
|-
| 08.03
+
| 08:04
|'''“ I walk” '''
+
|printed out. This is because you have invoked the two methods  “breathe”  and “walk”.
  
 
|-
 
|-
| 08.04
+
| 08:10
|printed out.  
+
|A “puts”  statement, defined within these '''methods''', gives the results you see.  
  
 
|-
 
|-
| 08.05
+
| 08:16
|This is because you have invoked the two methods ''' “breathe” ''' and ''' “walk”. '''
+
|Next, let us look at how to create '''methods''' with a trailing '''question mark (?).'''
 
+
  
 
|-
 
|-
| 08.10
+
| 08:21
|A '''“puts” ''' statement defined within these '''methods''' gives the results you see.  
+
|Create a new file in '''gedit''' as shown in the basic level '''Ruby tutorials'''.  
  
 
|-
 
|-
| 08.16
+
| 08:25
|Next, let us look at how to create '''methods''' with a trailing '''question mark.'''
+
|And name it '''class underscore methods underscore with underscore trailing underscore characters dot rb'''.
  
 
|-
 
|-
| 08.21
+
| 08:35
|Create a new file in '''gedit''' as shown in the basic level Ruby tutorials.  
+
|I have a working example of '''class methods with question mark''' code.  
  
 
|-
 
|-
| 08.25
+
| 08:40
|And name it '''class underscore methods underscore with underscore trailing underscore characters dot rb'''
+
|You can pause the tutorial and type the code as we go through it.
  
 
|-
 
|-
| 08.35
+
| 08:45
|I have a working example of '''class methods''' with '''question mark''' code.  
+
|Here, I have taken the same '''class''' as before, as an example.  
  
 
|-
 
|-
| 08.40
+
| 08:48
|You can pause the tutorial, and type the code as we go through it.  
+
|Here, the '''"breathe" method''' has a trailing question mark (?).  
  
 
|-
 
|-
| 08.45
+
| 08:52
|Here I have taken the same class as before as an example.  
+
|Such '''methods''' are generally used to return '''Boolean values.'''
  
 
|-
 
|-
| 08.48
+
| 08:55
|Here the '''breathe method''' has a trailing '''“question mark (?)”. '''
+
 
+
|-
+
| 08.52
+
|Such '''methods''' are generally used to return '''boolean values. '''
+
 
+
 
+
|-
+
| 08.55
+
 
|This is based on the '''method''' naming convention of Ruby.  
 
|This is based on the '''method''' naming convention of Ruby.  
  
 
|-
 
|-
| 09.00
+
| 09:00
|The '''method''' gets invoked by declaring '''animal dot breathe question-mark'''
+
|The '''method''' gets invoked by declaring '''animal dot breathe question-mark'''.
  
 
|-
 
|-
| 09.06
+
| 09:06
|Switch to the '''terminal''' and type  
+
|Switch to the '''terminal''' and type:
  
 
|-
 
|-
| 09.09
+
| 09:09
 
|'''ruby space class underscore methods underscore with underscore trailing underscore characters dot rb'''  and see the output.  
 
|'''ruby space class underscore methods underscore with underscore trailing underscore characters dot rb'''  and see the output.  
 
  
 
|-
 
|-
| 09.22
+
| 09:22
|You will notice the output as : ''' “true” '''
+
|You will notice the output as : ''' “true” '''
 
+
  
 
|-
 
|-
| 09.26
+
| 09:26
 
|Next, let us define another '''method''' called '''“walk”.'''
 
|Next, let us define another '''method''' called '''“walk”.'''
  
 
|-
 
|-
| 09.30
+
| 09:30
 
|Let us place an ''' equal-to''' sign ''' “=(value)”''' beside it.  
 
|Let us place an ''' equal-to''' sign ''' “=(value)”''' beside it.  
  
 
|-
 
|-
| 09.36
+
| 09:36
|Let us invoke this ''' method''' by calling ''' animal dot walk.'''  
+
|Let us invoke this ''' method''' by '''call'''ing '''animal dot walk'''.
  
 
|-
 
|-
| 09.41
+
| 09:41
|Then let us execute this '''method. '''
+
|Then let us execute this method.  
  
 
|-
 
|-
| 09.44
+
| 09:44
|Switch to the '''terminal''' and type  
+
|Switch to the '''terminal''' and type:
  
 
|-
 
|-
| 09.45
+
| 09:45
|'''ruby class underscore methods underscore with underscore trailing underscore characters dot rb '''
+
|'''ruby class underscore methods underscore with underscore trailing underscore characters dot rb'''
 
+
  
 
|-
 
|-
| 09.52
+
| 09:52
 
|and see the output.  
 
|and see the output.  
  
 
|-
 
|-
| 09.56
+
| 09:56
 
|It will give an ''' “undefined method” ''' error.  
 
|It will give an ''' “undefined method” ''' error.  
  
 
|-
 
|-
| 09.59
+
| 09:59
|This is because the ''' equal to sign''' has another meaning.  
+
|This is because, the equal to sign (=) has another meaning.  
  
 
|-
 
|-
| 10.03
+
| 10:03
 
|It is used to assign a value to a ''' method.'''
 
|It is used to assign a value to a ''' method.'''
  
 
|-
 
|-
| 10.08
+
| 10:08
 
|So, let us invoke the '''method''' a little differently this time.  
 
|So, let us invoke the '''method''' a little differently this time.  
  
 
|-
 
|-
| 10.13
+
| 10:13
|Type '''puts animal dot walk equal to “ hops” '''
+
|Type: '''puts animal dot walk equal to “hops”'''.
  
 
|-
 
|-
| 10.17
+
| 10:17
|Now let give it another try.  
+
|Now, let us give it another try.  
  
 
|-
 
|-
| 10.20
+
| 10:20
|Switch to the ''' terminal''' and run the command like before and see the output.  
+
|Switch to the ''' terminal''' and run the '''command''' like before and see the output.  
  
 
|-
 
|-
| 10.27
+
| 10:27
 
|You will notice that the word  '''“hops”''' get printed.  
 
|You will notice that the word  '''“hops”''' get printed.  
  
 
|-
 
|-
| 10.30
+
| 10:30
|This demonstrates that the ''' equal to sign''' next to a method means assignment.
+
|This demonstrates that the ''' equal to sign''' (=) next to a '''method''' means assignment.
  
 
|-
 
|-
| 10.36
+
| 10:36
 
|Now you should be able to write your own '''methods. '''
 
|Now you should be able to write your own '''methods. '''
  
 
|-
 
|-
| 10.42
+
| 10:42
|In this tutorial we have learnt-
+
|In this tutorial, we have learnt:
  
 
|-
 
|-
| 10.44
+
| 10:44
|How to declare '''classes '''
+
| How to declare '''classes'''
  
 
|-
 
|-
| 10.46
+
| 10:46
|How to create ''' objects''' of a ''' class '''
+
| How to create '''objects''' of a ''' class'''
  
 
|-
 
|-
| 10.48
+
| 10:48
|Different ways of defining '''methods''' in '''Ruby'''  
+
| Different ways of defining '''methods''' in '''Ruby'''.
  
 
|-
 
|-
| 10.52
+
| 10:52
 
|As an assignment:  
 
|As an assignment:  
  
 
|-
 
|-
| 10.54
+
| 10:54
|Define a '''class Product '''
+
|Define a '''class Product'''
  
 
|-
 
|-
| 10.56
+
| 10:56
 
|Define '''methods''' that you can use to get values of “myvar” and set values for “myvar”.  
 
|Define '''methods''' that you can use to get values of “myvar” and set values for “myvar”.  
  
 
|-
 
|-
| 11.01
+
| 11:01
 
|To set values, define the '''method''' using “=” sign.  
 
|To set values, define the '''method''' using “=” sign.  
  
 
|-
 
|-
|11.05
+
|11:05
|Instantiate the object of the class and set and get values using the above 2 methods.  
+
|Instantiate the object of the '''class''' and set and get values using the above 2 methods.  
 
+
  
 
|-
 
|-
| 11.12
+
| 11:12
 
|  Watch the video available at the following link.
 
|  Watch the video available at the following link.
  
 
|-
 
|-
| 11.14
+
| 11:14
|It summarises the Spoken Tutorial project.
+
|It summarizes the Spoken Tutorial project.
  
 
|-
 
|-
| 11.18
+
| 11:18
 
|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.  
  
 
|-
 
|-
|  11.22
+
|  11:22
|  The Spoken Tutorial Project Team :
+
|  The Spoken Tutorial project team:
  
 
|-   
 
|-   
| 11.24
+
| 11:24
|Conducts workshops using spoken tutorials  
+
| Conducts workshops using spoken tutorials.
  
 
|-
 
|-
|11.27
+
|11:27
|Gives certificates to those who pass an online test  
+
| Gives certificates to those who pass an online test.
  
 
|-
 
|-
| 11.30
+
| 11:30
|For more details, please write to contact@spoken-tutorial.org
+
|For more details, please write to: '''contact@spoken-tutorial.org'''
  
 
|-
 
|-
| 11.36
+
|11:36
| 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.
  
 
|-
 
|-
| 11.39
+
| 11:39
 
|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.
  
 
|-
 
|-
| 11.46
+
| 11:46
|More information on this Mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro
+
|More information on this mission is available at: '''spoken hyphen tutorial dot org slash NMEICT hyphen Intro'''.
  
 
|-
 
|-
| 11.56
+
| 11:56
| This is Anjana Nair signing off. Thank you
+
| This is Anjana Nair, signing off. Thank you.
  
 
|}
 
|}

Latest revision as of 12:37, 10 March 2017


Time Narration
00:01 Welcome to this spoken tutorial on Object Oriented Concept in Ruby.
00:06 In this tutorial, we will learn to:
00:08 use classes, creating objects
00:10 different ways of defining methods in Ruby.
00:13 Here we are using: Ubuntu version 12.04
00:16 Ruby 1.9.3
00:19 To follow this tutorial, you must have knowledge of Linux commands, Terminal and Text-editor.
00:24 If not, for relevant tutorials, please visit our website.
00:28 Before we begin, recall that we had created a “ttt” directory earlier.
00:33 Let's go to that directory.
00:35 To ruby hyphen tutorial and classes directory.
00:41 Ruby is an object oriented language.
00:44 Everything in Ruby is an object; from a value to a string or number.
00:49 A class is a collection of related data and functions.
00:53 It can serve to keep information organized.
00:56 An object is an instantiation of a class.
01:00 A class definition begins with the keyword class.
01:05 It is followed by the name of the class.
01:08 It is delimited with an “end”.
01:11 Let us look at an example of a class.
01:14 class Product
01:16 ruby code, end
01:20 The name of the class must begin with a capital letter.
01:24 Names that contain more than one word should be camelcased.
01:28 For example,
01:30 UserInformation
01:32 ProductInformation
01:34 The subsequent file names will have underscore separating the words:
01:37 user underscore information
01:40 product underscore information
01:45 Create a new file in gedit as shown in the basic level Ruby tutorials.
01:48 Name it class_definition.rb
01:52 I have a working example of the implementation of classes.
01:57 You can pause the tutorial and type the code as we go through it.
02:02 I have defined a class named Order in this example.
02:05 Now, let us make the class useful by adding some variables.
02:11 Then I have defined an instance variable “myinstance”.
02:15 And I have assigned a value to it.
02:18 I have also defined a class variable “myclassvar” .
02:21 And I have assigned a value to it.
02:24 Now, let us add some code that will make use of this class.
02:30 Type: puts Order dot instance underscore variables
02:36 Before this line, add puts some characters slash n for a new line.
02:43 Let us copy and paste that and add it just below the line we added and save it.
02:51 Now, let us execute this code.
02:53 Switch to the terminal and type:
02:56 ruby space class underscore definition dot rb
03:02 and see the output.
03:05 You will see the instance variable you defined.
03:09 Now, let us type: puts Order dot class underscore variables
03:15 Let us copy and paste the demarcation, just below the line and save it.
03:21 Now, let us switch to the terminal and execute the file like before.
03:26 You will notice the class variable you defined, also show up.
03:32 You should now be able to write your own class.
03:35 Next, let us look at what an object is.
03:40 An object is an instance of a class.
03:43 Which means, an object is created from a class.
03:46 An object will have the properties and methods defined in the class.
03:52 How do you declare an object?
03:54 We declare an object of a class using the "new" keyword.
03:58 Here, we are declaring an object of the Product class.
04:02 Here, an object gets created.
04:05 product = Product.new
04:09 This process is called initialization of an object.
04:12 This object is of type Product.
04:16 Now, let is look at what an “initialize” method is.
04:20 An initialize method is called at the time of object creation.
04:26 On calling "new" on an object, we invoke the initialize method.
04:31 An initialize method may take a list of parameters.
04:37 Like other 'Ruby' methods, it is preceded by the “def” keyword.
04:43 Let us look at an example.
04:46 Create a new file in gedit as shown in the basic level Ruby Tutorials.
04:50 And name it object underscore initialize dot rb.
04:55 I have a working example of the object initialization code.
05:00 You can pause the tutorial and type the code as we go through it.
05:04 Here, I have defined a class called “Order” .
05:08 Then I have defined the initialize method, with no argument.
05:13 I have defined a 'puts' method to display the message: “I have created an object”.
05:20 Next, I have defined Order dot new.
05:24 This will invoke the initialize method.
05:27 Switch to the terminal and type:
05:31 ruby space object underscore initialize dot rb
05:36 and see the output.
05:39 You will see the message: “I have created an object”.
05:43 Now, let us go back to gedit and add an argument to the method.
05:48 Let us modify the puts.
05:51 It should display the value of the argument passed.
05:55 Next, let us type: Order dot new(“I have created an object”).
06:04 Here, we have given an argument to the "new" method.
06:08 This argument gets passed on to the initialize method.
06:13 Switch to the terminal and type:
06:16 ruby space object underscore initialize dot rb
06:20 and see the output.
06:22 You will see the message: “I have created an object” printed out.
06:29 Now, you would have understood what object initialization means.
06:33 Recall that in Ruby, methods are the functions that a class performs.
06:39 Each method in a class is defined within the “def” and “end” block.
06:45 A multi-word method-name is separated with an underscore.
06:48 Some of the characters that can be appended to a method name are:
06:54 '?' (question-mark)
06:56 '=' (equal to).
06:58 Each of the characters add some meaning to the method.
07:02 Let us look at some examples.
07:05 Create a new file in gedit as shown in the basic level Ruby tutorials.
07:09 And name it class underscore methods dot rb.
07:14 I have a working example of class methods code.
07:17 You can pause the tutorial and type the code as we go through it.
07:21 Here, I have defined a class named “Animal”.
07:23 Then I have two methods- “breathe” and “walk”.
07:28 They are both defined with the “def” and “end” keywords.
07:32 I have then initialized the object 'Animal'.
07:36 I have assigned it to a variable called “animal” with lowercase “a”.
07:40 Then I have invoked the methods “breathe” and “walk” consecutively.
07:48 Now, let us execute the program.
07:51 Switch to the terminal and type:
07:53 ruby space class underscore methods dot rb
07:58 and see the output.
08:00 You will notice the lines:
08:02 “ I breathe” , “I walk”
08:04 printed out. This is because you have invoked the two methods “breathe” and “walk”.
08:10 A “puts” statement, defined within these methods, gives the results you see.
08:16 Next, let us look at how to create methods with a trailing question mark (?).
08:21 Create a new file in gedit as shown in the basic level Ruby tutorials.
08:25 And name it class underscore methods underscore with underscore trailing underscore characters dot rb.
08:35 I have a working example of class methods with question mark code.
08:40 You can pause the tutorial and type the code as we go through it.
08:45 Here, I have taken the same class as before, as an example.
08:48 Here, the "breathe" method has a trailing question mark (?).
08:52 Such methods are generally used to return Boolean values.
08:55 This is based on the method naming convention of Ruby.
09:00 The method gets invoked by declaring animal dot breathe question-mark.
09:06 Switch to the terminal and type:
09:09 ruby space class underscore methods underscore with underscore trailing underscore characters dot rb and see the output.
09:22 You will notice the output as : “true”
09:26 Next, let us define another method called “walk”.
09:30 Let us place an equal-to sign “=(value)” beside it.
09:36 Let us invoke this method by calling animal dot walk.
09:41 Then let us execute this method.
09:44 Switch to the terminal and type:
09:45 ruby class underscore methods underscore with underscore trailing underscore characters dot rb
09:52 and see the output.
09:56 It will give an “undefined method” error.
09:59 This is because, the equal to sign (=) has another meaning.
10:03 It is used to assign a value to a method.
10:08 So, let us invoke the method a little differently this time.
10:13 Type: puts animal dot walk equal to “hops”.
10:17 Now, let us give it another try.
10:20 Switch to the terminal and run the command like before and see the output.
10:27 You will notice that the word “hops” get printed.
10:30 This demonstrates that the equal to sign (=) next to a method means assignment.
10:36 Now you should be able to write your own methods.
10:42 In this tutorial, we have learnt:
10:44 How to declare classes
10:46 How to create objects of a class
10:48 Different ways of defining methods in Ruby.
10:52 As an assignment:
10:54 Define a class Product
10:56 Define methods that you can use to get values of “myvar” and set values for “myvar”.
11:01 To set values, define the method using “=” sign.
11:05 Instantiate the object of the class and set and get values using the above 2 methods.
11:12 Watch the video available at the following link.
11:14 It summarizes the Spoken Tutorial project.
11:18 If you do not have good bandwidth, you can download and watch it.
11:22 The Spoken Tutorial project team:
11:24 Conducts workshops using spoken tutorials.
11:27 Gives certificates to those who pass an online test.
11:30 For more details, please write to: contact@spoken-tutorial.org
11:36 Spoken Tutorial project is a part of the Talk to a Teacher project.
11:39 It is supported by the National Mission on Education through ICT, MHRD, Government of India.
11:46 More information on this mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro.
11:56 This is Anjana Nair, signing off. Thank you.

Contributors and Content Editors

Jyotisolanki, PoojaMoolya, Pratik kamble, Sandhya.np14, Shruti arya