Ruby/C3/Object-Oriented-Programming-Methods/English
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 |