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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "Test Page")
 
Line 1: Line 1:
Test Page
+
'''Title of script: Object Oriented concept in Ruby'''
 +
 
 +
'''Author: Anjana Nair'''
 +
 
 +
 
 +
'''Keywords: class'''
 +
 
 +
 
 +
 
 +
{| style="border-spacing:0;"
 +
| style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Visual Cue'''
 +
| style="border:0.05pt solid #000000;padding:0.097cm;"| '''Narration'''
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 1
 +
| 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;"| Welcome to this spoken tutorial on '''Object Oriented Programming – Methods '''in '''Ruby'''.
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 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;"| In this tutorial we will learn to use:
 +
 
 +
* '''instance methods'''
 +
* '''class methods'''
 +
* '''accessor methods'''
 +
 
 +
 
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 3
 +
| 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;"| Here we are using
 +
 
 +
* '''Ubuntu '''version 12.04
 +
* '''Ruby'''1.9.3
 +
 
 +
 
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 4
 +
 
 +
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.
 +
 
 +
 
 +
You must also have knowledge of '''Linux '''commands, '''Terminal '''and '''Text-editor.'''
 +
 
 +
 
 +
If not, for relevant tutorials, please visit our website.
 +
 
 +
|-
 +
| 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;"| Before we begin, recall that we had created “'''ttt'''” directory earlier.
 +
 
 +
 
 +
Let's go to that 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.'''
 +
 
 +
'''Create a directory names''' '''oop-methods and cd into it'''.
 +
 
 +
 
 +
 
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 5
 +
 
 +
What are '''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;"| 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'''.
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to '''gedit''' where you have already opened the file '''“instance_methods.rb” '''with the class defnition''' '''code typed inside.
 +
| 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;"| Create a new file in '''gedit '''as shown in the basic level '''Ruby '''tutorials.
 +
 
 +
 
 +
Name it '''instance_methods.rb'''
 +
 
 +
|-
 +
| 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 have a working example of the implementing '''instance methods'''.
 +
 
 +
 
 +
You can pause the tutorial, and type the code as we go through it.
 +
 
 +
|-
 +
| 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'''” method 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 defined a '''class '''named '''Product''' in this example.
 +
 
 +
 
 +
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: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.
 +
 
 +
|-
 +
| 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;"| '''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.'''
 +
 
 +
|-
 +
| 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 the logic we have.
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the poduct object creation 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;"| Here, I have initialized a '''Product''' '''object '''and named it as '''product_object_1'''.
 +
 
 +
 
 +
I have initialized it with a name Added. Pls validate.value and a price Added. Pls validate.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: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'''.
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight''' :'''
 +
 
 +
'''puts product_object_1.name '''
 +
 
 +
'''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'''.
 +
 
 +
 
 +
On invoking these '''methods,''' we get the values stored in the '''instance variables'''.
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| On the '''terminal '''type-
 +
 
 +
'''ruby instance_methods.rb >> '''press '''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;"| Now let us execute this code.
 +
 
 +
 
 +
Switch to the '''terminal''' and type:
 +
 
 +
'''ruby instance_methods.rb '''
 +
 
 +
 
 +
and press '''Enter '''to see the output.
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Point to the output.
 +
| 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 see that it will print the values you initialized the '''object''' with.
 +
 
 +
 
 +
Added. Pls validate.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:0.05pt solid #000000;padding:0.097cm;"| Now let us 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.
 +
 
 +
|-
 +
| 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: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: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.
 +
 
 +
 
 +
This proves that '''instance methods''' are available to all '''objects''' of the '''class Product'''.
 +
 
 +
|-
 +
| 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;"| You should now be able to write your own '''instance 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: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:none;padding:0.097cm;"| Slide 6
 +
 
 +
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.'''
 +
 
 +
 
 +
These '''methods '''are not available to instances of the '''class.'''
 +
 
 +
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: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:none;padding:0.097cm;"| Switch to '''gedit''' where you have already opened the file '''“class_methods.rb” '''with the class defnition''' '''code typed inside.
 +
| 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;"| Create a new file in '''gedit '''as shown in the basic level '''Ruby '''tutorials.
 +
 
 +
 
 +
Name it '''class_methods.rb'''
 +
 
 +
|-
 +
| 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 have a working example of '''class methods'''.
 +
 
 +
 
 +
You can pause the tutorial, and type the code as we go through it.
 +
 
 +
|-
 +
| 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: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.
 +
 
 +
 
 +
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.
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the first 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;"| 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'''.
 +
 
 +
|-
 +
| 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.
 +
 
 +
 
 +
Here I have used the '''self''' '''keyword'''.
 +
 
 +
|-
 +
| 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;"| Next, checkout the third way 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: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:0.05pt solid #000000;padding:0.097cm;"| Let us first initialize an object of '''Product''' like before.
 +
 
 +
 
 +
This time we are also giving a value for the '''description'''.
 +
 
 +
|-
 +
| 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: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.
 +
 
 +
 
 +
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: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: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 will print the values for '''name, price '''and''' description.'''
 +
 
 +
|-
 +
| 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.
 +
 
 +
 
 +
<nowiki><<PAUSE>></nowiki>
 +
 
 +
|-
 +
| 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 we shall see what '''accessor methods''' are.
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 5
 +
 
 +
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'''.
 +
 
 +
 
 +
Accessor methods comprise of '''setter methods''' and '''getter methods'''.
 +
 
 +
 
 +
'''Setter methods''' set the values.
 +
 
 +
 
 +
'''Getter methods''' get those values.
 +
 
 +
 
 +
'''Ruby''' uses the '''keyword''' '''attr_accessor''' to declare these '''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:0.05pt solid #000000;padding:0.097cm;"| Let us look at an example of '''accessor methods'''.
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to '''gedit''' where you have already opened the file '''“accessor_methods.rb” '''with the class defnition''' '''code typed inside.
 +
| 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;"| Create a new file in '''gedit '''as shown in the basic level '''Ruby '''tutorials.
 +
 
 +
 
 +
Name it '''accessor_methods.rb'''
 +
 
 +
|-
 +
| 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 have a working example of the implementing '''accessor methods'''.
 +
 
 +
 
 +
You can pause the tutorial, and type the code as we go through it.
 +
 
 +
|-
 +
| 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.
 +
 
 +
 
 +
I have declared '''attr_accessor '''for''' name and price.'''
 +
 
 +
 
 +
That is all that is required to use these types of '''methods'''.
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight 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;"| Now let us put it to action.
 +
 
 +
 
 +
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:0.05pt solid #000000;padding:0.097cm;"| 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.
 +
 
 +
|-
 +
| 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'''.
 +
 
 +
 
 +
These '''getter methods''' were also generated by the declaration of '''attr_accessor'''.
 +
 
 +
|-
 +
| 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 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;"|
 +
| 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 prints the values that were set.
 +
 
 +
|-
 +
| 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:none;padding:0.097cm;"| '''Slide'''
 +
 
 +
'''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;"| 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'''.
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide
 +
 
 +
Summary
 +
| 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;"| In this tutorial we have learnt about:
 +
 
 +
* '''instance methods'''
 +
* '''class methods '''and
 +
* '''accessor methods'''
 +
 
 +
 
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide
 +
 
 +
Assignment
 +
| 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;"| 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.'''
 +
 
 +
 
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide
 +
 
 +
About the Spoken Tutorial Project
 +
| 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;"| 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.
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide
 +
| 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
 +
 
 +
Gives certificates to those who pass an online test
 +
 
 +
For more details, please write to
 +
 
 +
contact at spoken hyphen tutorial dot org
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide
 +
 
 +
Acknowledgments
 +
| 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;"| 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.
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Previous Slide
 +
| 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;"| This is Anjana Nair signing off. Thank you
 +
 
 +
|}

Revision as of 14:08, 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 names 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 variablesname” 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 Added. Pls validate.value and a price Added. Pls validate.value.

Highlight the initialize block. The initializer block passes the values to the instance variables @name and @price.


Earlier we had studied about instance variables.

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.


Added. Pls validate.Namely, laptop and 35,000.

Next let us switch back to gedit.
Highlight the initialization of product_object_2 Now let us 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 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.
I did not get any output on executing this file.


Pls check the code.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 keyword 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