Difference between revisions of "Ruby/C3/Object-Oriented-Programming-Methods/English"
Nancyvarkey (Talk | contribs) |
|||
Line 23: | Line 23: | ||
* '''class methods''' | * '''class methods''' | ||
* '''accessor methods''' | * '''accessor methods''' | ||
− | |||
− | |||
|- | |- | ||
Line 32: | Line 30: | ||
* '''Ubuntu '''version 12.04 | * '''Ubuntu '''version 12.04 | ||
* '''Ruby'''1.9.3 | * '''Ruby'''1.9.3 | ||
− | |||
− | |||
|- | |- | ||
Line 39: | Line 35: | ||
Pre-requisites | Pre-requisites | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| To follow this tutorial, you must have a working '''Internet ''' | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| To follow this tutorial, you must have a working '''Internet '''connection. |
Line 57: | Line 53: | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the '''terminal''' which has all the commands for creating the directories and the prompt should be in '''oop-methods '''directory | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the '''terminal''' which has all the commands for creating the directories and the prompt should be in '''oop-methods '''directory | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then to '''ruby-tutorial.''' | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then to '''ruby-tutorial.''' | ||
− | |||
− | |||
− | |||
+ | Create a directory named '''oop-methods''' and cd into it. | ||
|- | |- | ||
Line 73: | Line 67: | ||
− | Earlier we had studied how to create '''objects '''or '''instances '''of a''' class'''. | + | Earlier we had studied how to create '''objects '''or '''instances ''' of a''' class'''. |
|- | |- | ||
Line 94: | Line 88: | ||
− | I have called an '''initialize | + | I have called an '''initialize method '''to initialize the '''instance variables "name"''' and '''"price"'''. |
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the methods “'''name'''” and “'''price'''” | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the methods “'''name'''” and “'''price'''” | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I have also defined '''instance methods '''named | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I have also defined '''instance methods '''named '''"name"''' and '''"price"'''. |
− | Each of them return '''instance variables | + | Each of them return '''instance variables "name"''' and '''"price"''' respectively. |
|- | |- | ||
Line 111: | Line 105: | ||
− | Shortly we will see how these '''methods '''will be available to all '''instances.''' | + | Shortly, we will see how these '''methods '''will be available to all '''instances.''' |
|- | |- | ||
Line 122: | Line 116: | ||
− | I have initialized it with a name | + | I have initialized it with a '''name value''' and a '''price value'''. |
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the '''initialize''' block. | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the '''initialize''' block. | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The '''initializer block''' passes the values to the '''instance variables | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The '''initializer block''' passes the values to the '''instance variables @name''' and '''@price'''. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 137: | Line 128: | ||
'''puts product_object_1.price''' | '''puts product_object_1.price''' | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now this '''product instance '''or '''object''' can use the''' instance methods | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now this '''product instance '''or '''object''' can use the''' instance methods name''' and '''price'''. |
Line 161: | Line 152: | ||
− | + | Namely, '''laptop '''and '''35,000'''. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the initialization of '''product_object_2''' | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the initialization of '''product_object_2''' | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Next, initialize another '''instance''' or '''object'''. |
Line 175: | Line 162: | ||
− | This time let us give a different set of values for name and price. | + | This time, let us give a different set of values for '''name''' and '''price'''. |
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the '''puts '''statements | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the '''puts '''statements | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us call the '''instance methods | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us call the '''instance methods "name"''' and '''"price"'''” for this '''object.''' |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the '''terminal '''>> Press up-arrow >> '''Enter.''' | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the '''terminal '''>> Press up-arrow >> '''Enter.''' | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Next let us switch back to the terminal and execute the code like before. | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Next let us switch back to the '''terminal''' and execute the code like before. |
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Output on the '''terminal.''' | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Output on the '''terminal.''' | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| You will notice that it executes successfully and prints out the new values. | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| You will notice that it executes successfully and it prints out the new values. |
Line 201: | Line 185: | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Next let us look at what '''class methods '''are. | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Next, let us look at what '''class methods '''are. |
|- | |- | ||
Line 207: | Line 191: | ||
What are '''class''' '''methods''' | What are '''class''' '''methods''' | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Class | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Class methods''' are '''methods '''available only to the '''class.''' |
− | These '''methods '''are not available to instances of the '''class.''' | + | These '''methods '''are not available to '''instances''' of the '''class.''' |
− | There are different ways you can define class methods. | + | There are different ways you can define '''class methods'''. |
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us look at an example. | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us look at an example. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 237: | Line 218: | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''Product.''' | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''Product.''' | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I have defined a '''Product | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I have defined a '''Product class''' like before. |
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the '''initialize''' code block. | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the '''initialize''' code block. | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I have also called an initializer like before. | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I have also called an '''initializer''' like before. |
However, this time I have added an extra '''argument''' called '''description'''. | However, this time I have added an extra '''argument''' called '''description'''. | ||
− | I am also using class variables to hold the values unlike instance variables earlier. | + | I am also using '''class variables''' to hold the values unlike '''instance variables''', earlier. |
|- | |- | ||
Line 256: | Line 237: | ||
− | Here it is defined using the '''class name Product'''. | + | Here it is defined using the '''class name "Product"'''. |
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the second class declaration code. | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the second class declaration code. | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then checkout the second '''class methods''' declaration. | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then, checkout the second '''class methods''' declaration. |
− | Here I have used the '''self | + | Here I have used the '''"self" keyword'''. |
|- | |- | ||
Line 271: | Line 252: | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us implement these '''class | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us implement these '''class methods'''. |
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the object creation logic. | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the object creation logic. | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us first initialize an object of '''Product''' like before. | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us first initialize an '''object''' of '''Product,''' like before. |
Line 282: | Line 263: | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the lines corresponding the class method invocation. | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the lines corresponding the class method invocation. | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us invoke the '''class | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us invoke the '''class methods''' as shown here. |
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us execute the code and inspect the output. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the '''terminal '''>> type '''ruby class_methods.rb''' >> '''Enter.''' | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the '''terminal '''>> type '''ruby class_methods.rb''' >> '''Enter.''' | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Switch to the terminal and execute the code like before. | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Switch to the '''terminal''' and execute the code like before. |
|- | |- | ||
Line 304: | Line 279: | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now you should be able to write your own class methods. | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now you should be able to write your own '''class methods'''. |
Line 317: | Line 292: | ||
What are '''accessor''' '''methods''' | What are '''accessor''' '''methods''' | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Ruby''' uses '''accessor | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| |
− | + | * '''Ruby''' uses '''accessor methods''' to access data defined within '''classes'''. | |
− | + | *'''Accessor methods''' comprise of '''setter methods''' and '''getter methods'''. | |
− | Accessor methods comprise of '''setter methods''' and '''getter methods'''. | + | *'''Setter methods''' set the values. |
− | + | *'''Getter methods''' get those values. | |
− | + | ||
− | '''Setter methods''' set the values. | + | |
− | + | ||
− | + | ||
− | '''Getter methods''' get those values. | + | |
− | '''Ruby''' uses the | + | '''Ruby''' uses the word '''attr_accessor''' to declare these '''methods'''. |
|- | |- | ||
Line 351: | Line 321: | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight “'''attr_accessor'''” line | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight “'''attr_accessor'''” line | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I have defined a '''class''' named '''Product''' in this example. | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I have defined a '''class''' named '''Product,''' in this example. |
Line 367: | Line 337: | ||
− | I have initialized a '''Product | + | I have initialized a '''Product object'''. |
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the '''setter''' logic. | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the '''setter''' logic. | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then I have set the '''name''' and '''price''' of the '''product object'''. | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then, I have set the '''name''' and '''price''' of the '''product object'''. |
Line 378: | Line 348: | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the '''getter''' logic. | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the '''getter''' logic. | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I have then attempted to print the values using the '''getter | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I have then attempted to print the values using the '''getter methods''' for '''name''' and '''price'''. |
Line 393: | Line 363: | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| By now you should be able to write your own '''accessor methods'''. | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| By now, you should be able to write your own '''accessor methods'''. |
|- | |- | ||
Line 413: | Line 383: | ||
* '''class methods '''and | * '''class methods '''and | ||
* '''accessor methods''' | * '''accessor methods''' | ||
− | |||
− | |||
|- | |- | ||
Line 423: | Line 391: | ||
* Define a '''class''' named '''Temperature''' | * Define a '''class''' named '''Temperature''' | ||
− | * Write an '''instance method '''using '''Ruby's | + | * Write an '''instance method '''using '''Ruby's accessor method''' syntax. |
* This '''method''' should calculate the '''Celsius '''for the given '''Fahrenheit.''' | * This '''method''' should calculate the '''Celsius '''for the given '''Fahrenheit.''' | ||
− | |||
− | |||
|- | |- | ||
Line 442: | Line 408: | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team : | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team : | ||
− | + | *Conducts workshops using spoken tutorials | |
− | Conducts workshops using spoken tutorials | + | *Gives certificates to those who pass an online test |
− | + | ||
− | Gives certificates to those who pass an online test | + | |
For more details, please write to | For more details, please write to |
Latest revision as of 17:59, 22 December 2014
Title of script: Object Oriented concept in Ruby
Author: Anjana Nair
Keywords: class
Visual Cue | Narration |
Slide 1 | Welcome to this spoken tutorial on Object Oriented Programming – Methods in Ruby. |
Slide 2 | In this tutorial we will learn to use:
|
Slide 3 | Here we are using
|
Slide 4
Pre-requisites |
To follow this tutorial, you must have a working Internet connection.
|
Before we begin, recall that we had created “ttt” directory earlier.
| |
Switch to the terminal which has all the commands for creating the directories and the prompt should be in oop-methods directory | Then to ruby-tutorial.
|
Slide 5
What are instance methods? |
What are Instance methods?
|
Switch to gedit where you have already opened the file “instance_methods.rb” with the class defnition code typed inside. | Create a new file in gedit as shown in the basic level Ruby tutorials.
|
I have a working example of the implementing instance methods.
| |
Highlight the “initialize” method block | I have defined a class named Product in this example.
|
Highlight the methods “name” and “price” | I have also defined instance methods named "name" and "price".
|
Instance methods are defined just like normal methods.
| |
Now let us implement the logic we have. | |
Highlight the poduct object creation code. | Here, I have initialized a Product object and named it as product_object_1.
|
Highlight the initialize block. | The initializer block passes the values to the instance variables @name and @price. |
Highlight :
puts product_object_1.name puts product_object_1.price |
Now this product instance or object can use the instance methods name and price.
|
On the terminal type-
ruby instance_methods.rb >> press Enter |
Now let us execute this code.
ruby instance_methods.rb
|
Point to the output. | You will see that it will print the values you initialized the object with.
|
Highlight the initialization of product_object_2 | Next, initialize another instance or object.
|
Highlight the puts statements | Now let us call the instance methods "name" and "price"” for this object. |
Switch to the terminal >> Press up-arrow >> Enter. | Next let us switch back to the terminal and execute the code like before. |
Output on the terminal. | You will notice that it executes successfully and it prints out the new values.
|
You should now be able to write your own instance methods. | |
Next, let us look at what class methods are. | |
Slide 6
What are class methods |
Class methods are methods available only to the class.
There are different ways you can define class methods. |
Let us look at an example. | |
Switch to gedit where you have already opened the file “class_methods.rb” with the class defnition code typed inside. | Create a new file in gedit as shown in the basic level Ruby tutorials.
|
I have a working example of class methods.
| |
Highlight Product. | I have defined a Product class like before. |
Highlight the initialize code block. | I have also called an initializer like before.
I am also using class variables to hold the values unlike instance variables, earlier. |
Highlight the first class declaration code. | This class will demonstrate to you the 3 different ways one can define class methods.
|
Highlight the second class declaration code. | Then, checkout the second class methods declaration.
|
Highlight the second class declaration code. | Next, checkout the third way you can define class methods. |
Now let us implement these class methods. | |
Highlight the object creation logic. | Let us first initialize an object of Product, like before.
|
Highlight the lines corresponding the class method invocation. | Now let us invoke the class methods as shown here. |
Now let us execute the code and inspect the output. | |
Switch to the terminal >> type ruby class_methods.rb >> Enter. | Switch to the terminal and execute the code like before. |
Output on the terminal. | You will notice that it will print the values for name, price and description. |
Now you should be able to write your own class methods.
| |
Next we shall see what accessor methods are. | |
Slide 5
What are accessor methods |
|
Let us look at an example of accessor methods. | |
Switch to gedit where you have already opened the file “accessor_methods.rb” with the class defnition code typed inside. | Create a new file in gedit as shown in the basic level Ruby tutorials.
|
I have a working example of the implementing accessor methods.
| |
Highlight “attr_accessor” line | I have defined a class named Product, in this example.
|
Highlight object creation logic.
|
Now let us put it to action.
|
Highlight the setter logic. | Then, I have set the name and price of the product object.
|
Highlight the getter logic. | I have then attempted to print the values using the getter methods for name and price.
|
Now let us execute the code like before. | |
You will notice that it prints the values that were set. | |
By now, you should be able to write your own accessor methods. | |
Slide
Accessor Methods |
One thing to note is that accessor methods are, by default, instance methods.
|
Slide
Summary |
In this tutorial we have learnt about:
|
Slide
Assignment |
As an assignment:
|
Slide
About the Spoken Tutorial Project |
Watch the video available at the following link.
It summarizes the Spoken Tutorial project. If you do not have good bandwidth, you can download and watch it. |
Slide | The Spoken Tutorial Project Team :
For more details, please write to contact at spoken hyphen tutorial dot org |
Slide
Acknowledgments |
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 hyphen tutorial dot org slash NMEICT hyphen Intro. |
Previous Slide | This is Anjana Nair signing off. Thank you |