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 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 classes directory | Then 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 separated by an “underscore (_)”. |
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 implementing 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 | 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 switch add some code that will make use of this class. to the terminal and tType puts Order dot instance underscore variables.
Before this line, “puts” some characters and slash n for a new line. This is done for readability. Let us copy and paste that and add it just below the line we added and save it.
Switch to the terminal and type ruby class_definition.rb and see the output.
|
Point to the output | Inspect the output.
You will see all the instance variable you defineds of class “Order”. From that list you can spot the one that you defined in the class. |
Not sure if this is a repetition of the above 2 rows.Next let us type execute Order.class_variables .
Let us copy and paste the demarcation just below the line and save it.and inspect the output. 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 | 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.new.
This will invoke the initialize method. |
On the terminal type
ruby object_initialize.rb |
Switch to the terminal and type
ruby object underscore initialize dot rb
|
Point to the output | You will see the message “I have created an object” printed out. |
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 code.
It should display the value of the argument passed. |
Visual Cue is missing
Highlight the line Order.new |
Next let us call type
Order.new(“I have initialized youcreated an object”). Here we have given an argument to the new method. This argument gets passed on to the initialize method. Next let us replace the string with “arg”. |
Switch to the terminal and type
ruby object_initialize.rb and see the output. | |
You will see the message “I have initialized youcreated 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 154 – 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”. These methods are invoked on lines 15 and 16 respectively.
|
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.breathe? |
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
|
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. The let us execute this method. |
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 the method.
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 type
ruby class underscore methods underscore with underscore trailing underscore characters dot rbrub the command like before.
|
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 15 – Defining accessor methods in a class | There are three ways of defining accessor methods in a class
attr_accessor |
Slide 15 contd... | * An attr_reader method is used to define an accessor method for an attribute of a class.
|
Switch to gedit | Create a new file in gedit as shown in the basic level Ruby tutorials.
|
Highlight the class definition.
Highlight the next line that has attr_reader |
I have defined a class Product like before.
Then I have declared attr_reader :name. This will generate a method that will return the value of “name”. Since “name” is not defined, this declaration is meaningless. It will add value only if you have a method that assigns a value to “name”. So, we have defined an attr_writer :name. |
Highlight the object initialization line.
Then highlight the puts line. |
Now let us initialize an object of class Product.
Then let us call the method “name” on it. Then we will use puts to print the value returned. |
On the terminal type
ruby class_attribute_accessors.rb |
Switch to the terminal and type
ruby class underscore attribute underscore accessors dot rb and see the output. |
Point to the terminal | There will be no output.
This is because the “name” attribute does not have any value assigned to it. |
Back on gedit
Type the code product.name = “ipad” |
So, now let us assign a value to “name”.
Type product.name = “ipad”. Now switch to the terminal again and execute the code like before.
|
Highlight the puts “The name of the product is” part of the code. | Let us modify the “puts” so that the sentence is more meaningful. |
Now let us execute the code and inspect the output. | |
Point to the output | The output will say “The name of the product is ipad”. |
Next let us define an attr_accessor for “price”. | |
Visual Cue missing
Is this a slide or will you point to some code |
Now let us use this accessor by defining
product.price. Since this method acts as a value setter and a getter, let us:
and get the value it contains |
Type the code.
puts “The price of the product is: “+ product.price.to_s |
Lets declare product.price = 50000.
Then let us use “puts” to print the value of price. So we type puts “The price of the product is: “+ product.price.to_s |
On the terminal type
ruby class_attribute_accessors.rb |
Switch to the terminal and type
ruby class underscore attribute underscore accessors dot rb and see the output. |
You will notice the string declared with “puts” gets displayed. | |
Now you should be able to write your own attribute accessors, readers and writers. | |
Slide 16 -- Assignment | As an assignment:
The output should be in the format: “The product was initialised with this name: #{product_object.prod_name}” |
Slide 16
Summary |
In this tutorial we have learnt-
|
Slide 17
Assingment |
As an assignment:
|
Slide 182
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 |