Ruby/C3/Object-Oriented-Concept-in-Ruby/English

From Script | Spoken-Tutorial
Jump to: navigation, search

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 Concept in Ruby.
Slide 2 In this tutorial we will learn to use-
  • classes
  • creating objects
  • different ways of defining methods in Ruby
Slide 3

System Requirements

Here we are using
  • Ubuntu version 12.04
  • Ruby1.9.3
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


The name of the class must begin with a capital letter.

Names that contain more than one word should be camelcased.

For example,

  • UserInformation
  • ProductInformation


The subsequent file names will have underscore separating the words:

  • user underscore information
  • product underscore information
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.


Name it class_definition.rb

I have a working example of the implementation of classes.


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

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.


On declaring calling new on an object, we invoke the initialize method.

Slide 13 An initialize method may take a list of parameters.


Like other Ruby methods, it is preceded by the “def” keyword.


Let us look at an example.

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.


And name it object_initialize.rb

I have a working example of the object initialization code.


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

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


and see the output.

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.


Each method in a class is defined within the “def” and “end” block.


A multiword method name is separated with an underscore.

Slide 15 – Defining methods in a class

contd...

Some of the characters that can be appended to a method name are:
  • ? (question-mark)
  • = (equal to)

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.


And name it class underscore methods dot rb

I have a working example of class methods code.


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

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.


Switch to the terminal and type

ruby class underscore methods dot rb


and see the output.

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”.


A “puts” statement defined within these methods gives the results you see.

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.


And name it class underscore methods underscore with underscore trailing underscore characters dot rb

In gedit I have a working example of class methods with question mark code.


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

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.


This is based on the method naming convention of Ruby.

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


and see the output.

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.


Then 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


and see the output.

Highlight the error


It will give an “undefined method” error.


This is because the equal to sign has another meaning.

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.


Type puts animal dot walk equal to “ hops”

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-
  • How to declare classes
  • How to create objects of a class
  • Different ways of defining methods in Ruby
Slide 17

Assingment

As an assignment:
  • Define a class Product
  • Define methods that you can use to get values of “myvar” and set values for “myvar”.
  • To set values, define the method using “=” sign.
  • Instantiate the object of the class and set and get values using the above 2 methods.
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 :


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 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

Contributors and Content Editors

Anjana, Nancyvarkey