Ruby/C3/Object-Oriented-Concept-in-Ruby/English
Title of script: Object Oriented concept in Ruby
Author: Anjana Nair
Keywords: class
|
|
Slide 1 | Welcome to this spoken tutorial on Object Oriented Concept in Ruby. |
Slide 2 | In this tutorial we will learn to use-
|
Slide 3
System Requirements |
Here we are using
|
Slide 4
Pre-requisites |
To follow this tutorial, you must 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 a “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 classes directory | To ruby-tutorial and classes directory. |
Slide 5 | Ruby is an object oriented language.
Everything in Ruby is an object; from a value to a string or number. |
Slide 6 – What is a Class in Ruby? | A class is a collection of related data and functions.
It can serve to keep information organized. An object is an instantiation of a class. |
Slide 7 | A class definition begins with the keyword class.
It is followed by the name of the class. It is delimited with an “end”. |
Let us look at an example of a class. | |
Slide 8 | class Product
ruby code end
Names that contain more than one word should be camelcased. For example,
|
Switch to gedit where you have already opened the file “class_definition.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 implementation of classes.
| |
Highlight “Order” | I have defined a class named Order in this example.
Now let us make the class useful by adding some variables. |
HIghlight “@myinstance” | Then I have defined an instance variable “myinstance”. |
Highlight the value | And I have assigned a value to it. |
Highlight “@myclassvar” | I have also defined a class variable “myclassvar”
And I have assigned a value to it. |
On the terminal type
Order.instance_variables |
Now let us add some code that will make use of this class.
Type puts Order dot instance underscore variables.
|
Type puts *********\n | Before this line, add puts some characters slash n for a new line. |
Copy and paste puts *********\n | Let us copy and paste that and add it just below the line we added and save it.
|
Now let us execute this code. | |
On the terminal, type
ruby class_definition.rb |
Switch to the terminal and type
ruby space class underscore definition dot rb and see the output. |
Point to the output | You will see the instance variable you defined. |
Now let us type puts Order dot class underscore dot variables .
Let us copy and paste the demarcation, just below the line and save it. | |
Switch to the terminal and press up-arrow for the previous command >> Press Enter | Now let us switch to the terminal and execute the file like before.
You will notice the class variable you defined, also show up. |
You should now be able to write your own class. | |
Next, let us look at what an object is. | |
Slide 9 – What is an object? | An object is an instance of a class.
Which means an object is created from a class. An object will have the properties and methods defined in the class. |
Slide 10 – How do you declare an object | How do you declare an object
We declare an object of a class using the new keyword. |
Slide 11 | Here we are declaring an object of the Product class.
Here an object gets created. product = Product.new This process is called initialization of an object. This object is of type : Product. |
Slide 12 | Now let is look at what an “initialize” method is.
An initialize method is called at the time of object creation.
|
Slide 13 | An initialize method may take a list of parameters.
|
Switch to gedit where you have already opened the file “object_initialize.rb” with the object initialize code typed inside. | Create a new file in gedit as shown in the basic level Ruby tutorials.
|
I have a working example of the object initialization code.
| |
Highlight class Order | Here I have defined a class called “Order” |
Highlight initialize method | Then I have defined the initialize method, with no argument. |
Highlight the puts declaration
|
I have defined a puts method to display the message “I have created an object”. |
Then highlight next line that declares the object. | Next, I have defined Order dot new.
This will invoke the initialize method. |
On the terminal type
ruby object_initialize.rb |
Switch to the terminal and type
ruby space object underscore initialize dot rb
|
Point to the output | You will see the message “I have created an object” . |
Switch to gedit. | Now let us go back to gedit and add an argument to the method. |
Highlight the code with puts. | Let us modify the puts .
It should display the value of the argument passed. |
Visual Cue is missing
Highlight the line Order.new |
Next let us type
Order dot new(“I have created an object”). Here we have given an argument to the new method. This argument gets passed on to the initialize method. |
Switch to the terminal and type
ruby space object underscore initialize dot rb and see the output. | |
You will see the message “I created an object” printed out.
Now, you would have understood what object initialization means. | |
Slide 14 – Defining methods in a class | Recall that in Ruby, methods are the functions that a class performs.
|
Slide 15 – Defining methods in a class
contd... |
Some of the characters that can be appended to a method name are:
Each of the characters add some meaning to the method. Let us look at some examples. |
Switch to gedit. | Create a new file in gedit as shown in the basic level Ruby tutorials.
|
I have a working example of class methods code.
| |
Highlight class Animal | Here I have defined a class named “Animal”. |
Then highlight methods breathe and walk | Then I have two methods “breathe” and “walk”.
They are both defined with the “def” and “end” keywords. |
Highlight Animal.new
Then highlight animal.breathe and then animal.walk |
I have then initialized the object Animal.
I have assigned it to a variable called “animal” with lowercase “a”. Then I have invoked the methods “breathe” and “walk” consequtively. |
On the terminal type
ruby class_methods.rb |
Now let us execute the program.
ruby class underscore methods dot rb
|
Highlight the output. | You will notice the lines:
“ I breathe” “ I walk” printed out. This is because you have invoked the two methods “breathe” and “walk”.
|
Next, let us look at how to create methods with a trailing question mark. | |
Switch to gedit. | Create a new file in gedit as shown in the basic level Ruby tutorials.
|
In gedit | I have a working example of class methods with question mark code.
|
Highlight “breathe?” line | Here I have taken the same class as before as an example.
Here the breathe method has a trailing “question mark (?)”. Such methods are generally used to return boolean values.
|
Highlight animal.breathe? | The method gets invoked by declaring animal dot breathe question-mark |
On the terminal type
ruby class_methods_with_trailing_characters.rb |
Switch to the terminal and type
ruby space class underscore methods underscore with underscore trailing underscore characters dot rb
|
You will notice the output as :
“true” | |
Highlight “walk=(value)” method | Next, let us define another method called “walk”.
Let us place an equal-to sign (“=(value)”) beside it. Llet us invoke this method by calling animal dot walk.
|
On the terminal type
ruby class_methods_with_trailing_characters.rb |
Switch to the terminal and type
ruby class underscore methods underscore with underscore trailing underscore characters dot rb
|
Highlight the error
|
It will give an “undefined method” error.
|
Type the code
animal.walk = “ hops” |
It is used to assign a value to amethod.
So, let us invoke the method a little differently this time.
|
Now let give it another try. | |
On the terminal type
ruby class_methods_with_trailing_characters.rb |
Switch to the terminal and run the command like before and see the output. |
On the terminal | You will notice that the word “hops” get printed. |
This demonstrates that the equal to sign next to a method means assignment.
Now you should be able to write your own methods. | |
Slide 16
Summary |
In this tutorial we have learnt-
|
Slide 17
Assingment |
As an assignment:
|
Slide 18
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 193 | The Spoken Tutorial Project Team :
Gives certificates to those who pass an online test For more details, please write to contact at spoken hyphen tutorial dot org |
Slide 2014
Acknowledgements |
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 |