Difference between revisions of "Ruby/C3/Object-Oriented-Programming-Methods/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
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 '''Connection.
+
| 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 names''' '''oop-methods and cd into it'''.
 
 
  
  
 +
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''' '''method '''to initialize the '''instance variables''' “'''name'''and '''price'''.
+
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 '''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 '''"name"''' and '''"price"'''.
  
  
Each of them return '''instance variables''' '''“@name'''and '''“@price'''respectively.
+
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 Added. Pls validate.value and a price Added. Pls validate.value.
+
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''' '''@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;"| The '''initializer block''' passes the values to the '''instance variables @name''' and '''@price'''.
 
+
 
+
Earlier we had studied about '''instance''' '''variables'''.
+
  
 
|-
 
|-
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''' '''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;"| Now this '''product instance '''or '''object''' can use the''' instance methods name''' and '''price'''.
  
  
Line 161: Line 152:
  
  
Added. Pls validate.Namely, '''laptop '''and '''35,000'''.
+
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;"|
+
| 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 '''gedit'''.
+
  
 
|-
 
|-
 
| 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;"| Now let us initialize another '''instance''' or '''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;"| 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 '''“'''name'''and '''price'''” for this '''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;"| 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''' '''methods''' are '''methods '''available only to 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;"| '''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''' '''class''' 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 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''' '''keyword'''.
+
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''' '''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 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''' '''methods''' as shown here.
+
| 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;"| I did not get any output on executing this file.
+
| 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.
 
+
 
+
Pls check the code.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''' '''methods''' to access data defined within '''classes'''.
+
| 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 '''keyword''' '''attr_accessor''' to declare these '''methods'''.
+
'''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''' '''object'''.
+
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''' '''methods''' for '''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 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''' '''accessor method''' syntax.
+
* 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:
  • instance methods
  • class methods
  • accessor methods
Slide 3 Here we are using
  • Ubuntu version 12.04
  • Ruby1.9.3
Slide 4

Pre-requisites

To follow this tutorial, you must have a working Internet connection.


You must also have knowledge of Linux commands, Terminal and Text-editor.


If not, for relevant tutorials, please visit our website.

Before we begin, recall that we had created “ttt” directory earlier.


Let's go to that directory.

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.


Create a directory named oop-methods and cd into it.

Slide 5

What are instance methods?

What are Instance methods?


Instance methods are those methods that are available to all instances of the class.


Earlier we had studied how to create objects or instances of a class.

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.


Name it instance_methods.rb

I have a working example of the implementing instance methods.


You can pause the tutorial, and type the code as we go through it.

Highlight the “initialize” method block I have defined a class named Product in this example.


I have called an initialize method to initialize the instance variables "name" and "price".

Highlight the methods “name” and “price I have also defined instance methods named "name" and "price".


Each of them return instance variables "name" and "price" respectively.

Instance methods are defined just like normal methods.


Earlier we had studied how to create methods in Ruby.


Shortly, we will see how these methods will be available to all instances.

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.


I have initialized it with a name value and a price value.

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 invoking these methods, we get the values stored in the instance variables.

On the terminal type-

ruby instance_methods.rb >> press Enter

Now let us execute this code.


Switch to the terminal and type:

ruby instance_methods.rb


and press Enter to see the output.

Point to the output. You will see that it will print the values you initialized the object with.


Namely, laptop and 35,000.

Highlight the initialization of product_object_2 Next, initialize another instance or object.


Let us name this object product_object_2.


This time, let us give a different set of values for name and price.

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.


This proves that instance methods are available to all objects of the class Product.

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.


These methods are not available to instances of 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.


Name it class_methods.rb

I have a working example of class methods.


You can pause the tutorial, and type the code as we go through it.

Highlight Product. I have defined a Product class like before.
Highlight the initialize code block. I have also called an initializer like before.


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.

Highlight the first class declaration code. This class will demonstrate to you the 3 different ways one can define class methods.


Checkout the class method declaration for name.


Here it is defined using the class name "Product".

Highlight the second class declaration code. Then, checkout the second class methods declaration.


Here I have used the "self" keyword.

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.


This time we are also giving a value for the description.

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.


<<PAUSE>>

Next we shall see what accessor methods are.
Slide 5

What are accessor methods

  • Ruby uses accessor methods to access data defined within classes.
  • Accessor methods comprise of setter methods and getter methods.
  • Setter methods set the values.
  • Getter methods get those values.


Ruby uses the word attr_accessor to declare these 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.


Name it accessor_methods.rb

I have a working example of the implementing accessor methods.


You can pause the tutorial, and type the code as we go through it.

Highlight “attr_accessor” line I have defined a class named Product, in this example.


I have declared attr_accessor for name and price.


That is all that is required to use these types of methods.

Highlight object creation logic.


Now let us put it to action.


I have initialized a Product object.

Highlight the setter logic. Then, I have set the name and price of the product object.


This is possible because the attr_declaration, by default, creates methods for setting values.

Highlight the getter logic. I have then attempted to print the values using the getter methods for name and price.


These getter methods were also generated by the declaration of attr_accessor.

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.


Thus they can be accessed by different instances of the class Product.

Slide

Summary

In this tutorial we have learnt about:
  • instance methods
  • class methods and
  • accessor methods
Slide

Assignment

As an assignment:
  • Define a class named Temperature
  • Write an instance method using Ruby's accessor method syntax.
  • This method should calculate the Celsius for the given Fahrenheit.
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 :
  • Conducts workshops using spoken tutorials
  • Gives certificates to those who pass an online test

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

Contributors and Content Editors

Anjana, Nancyvarkey