Difference between revisions of "Ruby/C3/Object-Oriented-Programming-Methods/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
Line 6: | Line 6: | ||
|- | |- | ||
| | 00:01 | | | 00:01 | ||
− | | | Welcome to this spoken tutorial on '''Object Oriented Programming – Methods '''in '''Ruby'''. | + | | | Welcome to this '''spoken tutorial''' on '''Object Oriented Programming – Methods '''in '''Ruby'''. |
|- | |- | ||
| | 00:07 | | | 00:07 | ||
− | | | In this tutorial we will learn to use: | + | | | In this tutorial, we will learn to use: |
|- | |- | ||
| | 00:09 | | | 00:09 | ||
− | | | '''instance methods''' | + | | |* '''instance methods''' |
|- | |- | ||
| | 00:11 | | | 00:11 | ||
− | | |'''class methods''' | + | | |* '''class methods''' |
|- | |- | ||
| | 00:14 | | | 00:14 | ||
− | | | | + | | |* '''accessor methods'''. |
|- | |- | ||
| | 00:15 | | | 00:15 | ||
− | | | Here we are using '''Ubuntu '''version 12.04 | + | | | Here, we are using: |
+ | * '''Ubuntu '''version '''12.04''' | ||
|- | |- | ||
| | 00:19 | | | 00:19 | ||
− | | | '''Ruby | + | | |* '''Ruby 1.9.3''' |
|- | |- | ||
Line 38: | Line 39: | ||
|- | |- | ||
| | 00:27 | | | 00:27 | ||
− | | |You must also have knowledge of '''Linux ''' | + | | |You must also have knowledge of '''Linux command'''s, '''Terminal '''and '''Text-editor.''' |
|- | |- | ||
Line 74: | Line 75: | ||
|- | |- | ||
| | 01:09 | | | 01:09 | ||
− | | | Create a new file in '''gedit '''as shown in the basic level '''Ruby ''' | + | | | Create a new file in '''gedit '''as shown in the basic level '''Ruby tutorials'''. |
|- | |- | ||
| | 01:14 | | | 01:14 | ||
− | | |Name it '''instance_methods.rb''' | + | | |Name it '''instance_methods.rb'''. |
|- | |- | ||
| | 01:19 | | | 01:19 | ||
− | | | I have a working example of | + | | | I have a working example of implementing '''instance methods'''. |
|- | |- | ||
| | 01:24 | | | 01:24 | ||
− | | |You can pause the tutorial | + | | |You can pause the tutorial and type the code as we go through it. |
|- | |- | ||
Line 94: | Line 95: | ||
|- | |- | ||
| | 01:33 | | | 01:33 | ||
− | | | I have called an '''initialize | + | | | I have called an '''initialize''' method to initialize the '''instance variables''', "name" and "price". |
|- | |- | ||
| | 01:41 | | | 01:41 | ||
− | | | I have also defined '''instance methods '''named | + | | | I have also defined '''instance methods '''named "name" and "price". |
|- | |- | ||
| | 01:47 | | | 01:47 | ||
− | | |Each of them return '''instance variables "name" | + | | |Each of them return '''instance variables''' "name" and "price" respectively. |
|- | |- | ||
Line 110: | Line 111: | ||
|- | |- | ||
| | 01:58 | | | 01:58 | ||
− | | |Earlier we had studied how to create | + | | |Earlier we had studied how to create methods in Ruby. |
|- | |- | ||
| | 02:02 | | | 02:02 | ||
− | | |Shortly, we will see how these | + | | |Shortly, we will see how these methods will be available to all '''instances.''' |
|- | |- | ||
| | 02:07 | | | 02:07 | ||
− | | | Now let us implement the logic we have. | + | | | Now, let us implement the '''logic''' we have. |
|- | |- | ||
| | 02:11 | | | 02:11 | ||
− | | | Here, I have initialized a '''Product | + | | | Here, I have initialized a '''Product''' object and named it as '''product_object_1'''. |
|- | |- | ||
Line 134: | Line 135: | ||
|- | |- | ||
| | 02:31 | | | 02:31 | ||
− | | | Now this '''product instance '''or '''object''' can use the''' | + | | | Now, this '''product instance '''or '''object''' can use the instance methods '''name''' and '''price'''. |
|- | |- | ||
| | 02:37 | | | 02:37 | ||
− | | |On invoking these | + | | |On invoking these methods, we get the values stored in the '''instance variables'''. |
|- | |- | ||
| | 02:43 | | | 02:43 | ||
− | | | Now let us execute this code. | + | | | Now, let us '''execute''' this code. |
|- | |- | ||
Line 170: | Line 171: | ||
|- | |- | ||
| | 03:23 | | | 03:23 | ||
− | | | Now let us call the '''instance methods "name" | + | | | Now, let us call the '''instance methods- "name" and "price" for this '''object.''' |
|- | |- | ||
| | 03:35 | | | 03:35 | ||
− | | | Next let us switch back to the '''terminal''' and execute the code like before. | + | | | Next, let us switch back to the '''terminal''' and execute the code like before. |
|- | |- | ||
Line 210: | Line 211: | ||
|- | |- | ||
| | 04:18 | | | 04:18 | ||
− | | | Create a new file in '''gedit '''as shown in the basic level '''Ruby ''' | + | | | Create a new file in '''gedit '''as shown in the basic level '''Ruby tutorials'''. |
|- | |- | ||
| | 04:24 | | | 04:24 | ||
− | | |Name it '''class_methods.rb''' | + | | |Name it '''class_methods.rb'''. |
|- | |- | ||
Line 222: | Line 223: | ||
|- | |- | ||
| | 04:32 | | | 04:32 | ||
− | | |You can pause the tutorial | + | | |You can pause the tutorial and type the code as we go through it. |
|- | |- | ||
Line 238: | Line 239: | ||
|- | |- | ||
| | 04:48 | | | 04:48 | ||
− | | |I am also using '''class variables''' to hold the values unlike '''instance variables''' | + | | |I am also using '''class variables''' to hold the values, unlike '''instance variables''' earlier. |
|- | |- | ||
Line 250: | Line 251: | ||
|- | |- | ||
| | 05:06 | | | 05:06 | ||
− | | |Here it is defined using the '''class name "Product"'''. | + | | |Here, it is defined using the '''class name "Product"'''. |
|- | |- | ||
Line 278: | Line 279: | ||
|- | |- | ||
| | 05:37 | | | 05:37 | ||
− | | | Now let us invoke the '''class methods''' as shown here. | + | | | Now, let us invoke the '''class methods''' as shown here. |
|- | |- | ||
| | 05:42 | | | 05:42 | ||
− | | | Now let us execute the code and inspect the output. | + | | | Now, let us execute the code and inspect the output. |
|- | |- | ||
Line 294: | Line 295: | ||
|- | |- | ||
| | 05:59 | | | 05:59 | ||
− | | | Now you should be able to write your own '''class methods'''. | + | | | Now, you should be able to write your own '''class methods'''. |
|- | |- | ||
Line 326: | Line 327: | ||
|- | |- | ||
| | 06:35 | | | 06:35 | ||
− | | | Create a new file in '''gedit '''as shown in the basic level '''Ruby ''' | + | | | Create a new file in '''gedit '''as shown in the basic level '''Ruby tutorials'''. |
|- | |- | ||
Line 338: | Line 339: | ||
|- | |- | ||
| | 06:47 | | | 06:47 | ||
− | | |You can pause the tutorial | + | | |You can pause the tutorial and type the code as we go through it. |
|- | |- | ||
Line 346: | Line 347: | ||
|- | |- | ||
| | 06:56 | | | 06:56 | ||
− | | |I have declared '''attr_accessor '''for''' name and price.''' | + | | |I have declared '''attr_accessor '''for''' name''' and '''price.''' |
|- | |- | ||
Line 362: | Line 363: | ||
|- | |- | ||
| | 07:10 | | | 07:10 | ||
− | | | Then, I have set the '''name''' and '''price''' of the '''product | + | | | Then, I have set the '''name''' and '''price''' of the '''product''' object. |
|- | |- | ||
Line 378: | Line 379: | ||
|- | |- | ||
| | 07:35 | | | 07:35 | ||
− | | | Now let us execute the code like before. | + | | | Now let us '''execute''' the code like before. |
|- | |- | ||
Line 398: | Line 399: | ||
|- | |- | ||
| | 08:00 | | | 08:00 | ||
− | | | In this tutorial we have learnt about: '''instance methods''', '''class methods '''and '''accessor methods''' | + | | | In this tutorial, we have learnt about: '''instance methods''', '''class methods '''and '''accessor methods'''. |
|- | |- | ||
| | 08:06 | | | 08:06 | ||
− | | | As an assignment: | + | | | As an assignment: define a '''class''' named '''Temperature'''. |
|- | |- | ||
| | 08:10 | | | 08:10 | ||
− | | | Write an '''instance method '''using ''' | + | | | Write an '''instance method '''using Ruby's '''accessor method''' syntax. |
|- | |- | ||
Line 426: | Line 427: | ||
|- | |- | ||
| |08:30 | | |08:30 | ||
− | | | The Spoken Tutorial | + | | | The Spoken Tutorial project team: |
|- | |- | ||
| | 08:31 | | | 08:31 | ||
− | | |Conducts workshops using spoken tutorials | + | | |* Conducts workshops using spoken tutorials. |
|- | |- | ||
| | 08:34 | | | 08:34 | ||
− | | |Gives certificates to those who pass an online test | + | | |* Gives certificates to those who pass an online test. |
|- | |- | ||
| | 08:38 | | | 08:38 | ||
− | | |For more details, please write to contact at spoken hyphen tutorial dot org | + | | |For more details, please write to: |
+ | contact at spoken hyphen tutorial dot org. | ||
|- | |- | ||
| | 08:44 | | | 08:44 | ||
− | | | Spoken Tutorial | + | | | '''Spoken Tutorial''' project is a part of the '''Talk to a Teacher''' project. |
|- | |- | ||
Line 450: | Line 452: | ||
|- | |- | ||
| | 08:55 | | | 08:55 | ||
− | | |More information on this | + | | |More information on this mission is available at: |
+ | spoken hyphen tutorial dot org slash NMEICT hyphen Intro. | ||
|- | |- | ||
| | 09:03 | | | 09:03 | ||
− | | | This is Anjana Nair signing off. Thank you | + | | | This is Anjana Nair, signing off. Thank you. |
|} | |} |
Revision as of 08:34, 30 October 2015
Time | Narration |
00:01 | Welcome to this spoken tutorial on Object Oriented Programming – Methods in Ruby. |
00:07 | In this tutorial, we will learn to use: |
00:09 | * instance methods |
00:11 | * class methods |
00:14 | * accessor methods. |
00:15 | Here, we are using:
|
00:19 | * Ruby 1.9.3 |
00:22 | To follow this tutorial, you must have a working Internet connection. |
00:27 | You must also have knowledge of Linux commands, Terminal and Text-editor. |
00:31 | If not, for relevant tutorials, please visit our website. |
00:36 | Before we begin, recall that we had created “ttt” directory earlier. |
00:41 | Let's go to that directory. |
00:44 | Then to ruby-tutorial. |
00:47 | Create a directory named oop-methods and cd into it. |
00:54 | What are Instance methods? |
00:56 | Instance methods are those methods that are available to all instances of the class. |
01:03 | Earlier we had studied how to create objects or instances of a class. |
01:09 | Create a new file in gedit as shown in the basic level Ruby tutorials. |
01:14 | Name it instance_methods.rb. |
01:19 | I have a working example of implementing instance methods. |
01:24 | You can pause the tutorial and type the code as we go through it. |
01:29 | I have defined a class named Product in this example. |
01:33 | I have called an initialize method to initialize the instance variables, "name" and "price". |
01:41 | I have also defined instance methods named "name" and "price". |
01:47 | Each of them return instance variables "name" and "price" respectively. |
01:54 | Instance methods are defined just like normal methods. |
01:58 | Earlier we had studied how to create methods in Ruby. |
02:02 | Shortly, we will see how these methods will be available to all instances. |
02:07 | Now, let us implement the logic we have. |
02:11 | Here, I have initialized a Product object and named it as product_object_1. |
02:18 | I have initialized it with a name value and a price value. |
02:24 | The initializer block passes the values to the instance variables @name and @price. |
02:31 | Now, this product instance or object can use the instance methods name and price. |
02:37 | On invoking these methods, we get the values stored in the instance variables. |
02:43 | Now, let us execute this code. |
02:46 | Switch to the terminal and type: ruby instance_methods.rb and press Enter to see the output. |
02:56 | You will see that it will print the values you initialized the object with. |
03:02 | Namely, laptop and 35,000. |
03:07 | Next, initialize another instance or object. |
03:12 | Let us name this object product_object_2. |
03:18 | This time, let us give a different set of values for name and price. |
03:23 | Now, let us call the instance methods- "name" and "price" for this object. |
03:35 | Next, let us switch back to the terminal and execute the code like before. |
03:41 | You will notice that it executes successfully and it prints out the new values. |
03:48 | This proves that instance methods are available to all objects of the class Product. |
03:55 | You should now be able to write your own instance methods. |
03:59 | Next, let us look at what class methods are. |
04:04 | Class methods are methods available only to the class. |
04:09 | These methods are not available to instances of the class. |
04:14 | There are different ways you can define class methods. |
04:16 | Let us look at an example. |
04:18 | Create a new file in gedit as shown in the basic level Ruby tutorials. |
04:24 | Name it class_methods.rb. |
04:28 | I have a working example of class methods. |
04:32 | You can pause the tutorial and type the code as we go through it. |
04:36 | I have defined a Product class like before. |
04:40 | I have also called an initializer like before. |
04:44 | However, this time I have added an extra argument called description. |
04:48 | I am also using class variables to hold the values, unlike instance variables earlier. |
04:55 | This class will demonstrate to you the 3 different ways one can define class methods. |
05:01 | Checkout the class method declaration for name. |
05:06 | Here, it is defined using the class name "Product". |
05:10 | Then, checkout the second class methods declaration. |
05:14 | Here I have used the "self" keyword. |
05:18 | Next, checkout the third way you can define class methods. |
05:23 | Now let us implement these class methods. |
05:27 | Let us first initialize an object of Product, like before. |
05:32 | This time we are also giving a value for the description. |
05:37 | Now, let us invoke the class methods as shown here. |
05:42 | Now, let us execute the code and inspect the output. |
05:47 | Switch to the terminal and execute the code like before. |
05:54 | You will notice that it will print the values for name, price and description. |
05:59 | Now, you should be able to write your own class methods. |
06:03 | Next we shall see what accessor methods are. |
06:07 | Ruby uses accessor methods to access data defined within classes. |
06:13 | Accessor methods comprise of setter methods and getter methods. |
06:18 | Setter methods set the values. |
06:22 | Getter methods get those values. |
06:24 | Ruby uses the word attr_accessor to declare these methods. |
06:31 | Let us look at an example of accessor methods. |
06:35 | Create a new file in gedit as shown in the basic level Ruby tutorials. |
06:39 | Name it accessor_methods.rb |
06:43 | I have a working example of the implementing accessor methods. |
06:47 | You can pause the tutorial and type the code as we go through it. |
06:52 | I have defined a class named Product, in this example. |
06:56 | I have declared attr_accessor for name and price. |
07:01 | That is all that is required to use these types of methods. |
07:05 | Now let us put it to action. |
07:07 | I have initialized a Product object. |
07:10 | Then, I have set the name and price of the product object. |
07:14 | This is possible because the attr_declaration, by default, creates methods for setting values. |
07:22 | I have then attempted to print the values using the getter methods for name and price. |
07:28 | These getter methods were also generated by the declaration of attr_accessor. |
07:35 | Now let us execute the code like before. |
07:40 | You will notice that it prints the values that were set. |
07:44 | By now, you should be able to write your own accessor methods. |
07:50 | One thing to note is that accessor methods are, by default, instance methods. |
07:55 | Thus they can be accessed by different instances of the class Product. |
08:00 | In this tutorial, we have learnt about: instance methods, class methods and accessor methods. |
08:06 | As an assignment: define a class named Temperature. |
08:10 | Write an instance method using Ruby's accessor method syntax. |
08:15 | This method should calculate the Celsius for the given Fahrenheit. |
08:20 | Watch the video available at the following link. |
08:23 | It summarizes the Spoken Tutorial project. |
08:26 | If you do not have good bandwidth, you can download and watch it. |
08:30 | The Spoken Tutorial project team: |
08:31 | * Conducts workshops using spoken tutorials. |
08:34 | * Gives certificates to those who pass an online test. |
08:38 | For more details, please write to:
contact at spoken hyphen tutorial dot org. |
08:44 | Spoken Tutorial project is a part of the Talk to a Teacher project. |
08:48 | 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:
spoken hyphen tutorial dot org slash NMEICT hyphen Intro. |
09:03 | This is Anjana Nair, signing off. Thank you. |