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

From Script | Spoken-Tutorial
Revision as of 09:49, 12 March 2014 by Anjana (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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
  • accessordifferent ways of defining methods in ruby and other kinds of methods


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


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

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.


Name it class_definition.rb

I have a working example of the implementing 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 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.


Now let us execute this code.

Switch to the terminal and type ruby class_definition.rb and see the output.


This is to see the instance variables of the class “Order”.

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.


In our previous examples we declared objects.


On declaringcalling new on an object we have invoked 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.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


and see the output.

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


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

Recall that in Ruby, methods are the functions that a class performs.


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


A multiword method name is separated with an underscore.

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.


And name it class_methods.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”.

These methods are invoked on lines 15 and 16 respectively.


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


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.

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


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 the method.

So, let us invoke the method a little differently this time.


animal.walk = “ hops”

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.


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.


This script is too long. I think you should cut it into 2 topics, with the first topic ending here.<PAUSE>

Slide 15 – Defining accessor methods in a class There are three ways of defining accessor methods in a class
  • attr_reader
  • attr_writer

attr_accessor

Slide 15 contd... * An attr_reader method is used to define an accessor method for an attribute of a class.
  • An attr_write method is used to define a setter method for an attribute of a class. This method enables you to set a value for the specified attribute.
  • An attr_accessor method can define both a getter and a setter method for the specified attribute. By default, the name of the methods that are generated will be the same name as the attribute.
  • Let us look at an example.


Switch to gedit Create a new file in gedit as shown in the basic level Ruby tutorials.


And name it class underscore attribute underscore accessors dot rb

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.


This time you will notice the value getting returned.

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:

  • assign a value to the price attribute

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:
  • Define a class called Product
  • Define an attr_accessor for name of the product.
  • During object intialization use the accessor methods to set and get value for “name”.
  • In the same object initialization method assign the value of “name” to an instance variable.
  • Define a separate method to access this instance variable.

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


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