Difference between revisions of "Ruby/C3/Object-Oriented-Concept-in-Ruby/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Classes and Object Oriented Concept in Ruby)
 
 
Line 8: Line 8:
  
  
{| style="border-spacing:0;"
+
{|border=1
| style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Cue'''</center>
+
| <center>'''Visual Cue'''</center>
| style="border:0.05pt solid #000000;padding:0.097cm;"| <center>'''Narration'''</center>
+
| <center>'''Narration'''</center>
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 1
+
|Slide 1
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Welcome to this spoken tutorial on '''Object Oriented Concept '''in '''Ruby.'''
+
|Welcome to this spoken tutorial on '''Object Oriented Concept '''in '''Ruby.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 2
+
|Slide 2
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| In this tutorial we will learn to use-
+
|In this tutorial we will learn to use-
 
+
 
* '''classes'''
 
* '''classes'''
* '''creating objects'''
+
* creating '''objects'''
* '''accessordifferent ways of defining methods in ruby '''and other kinds of''' methods'''
+
* different ways of defining '''methods''' in '''Ruby '''
 
+
 
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 3
+
|Slide 3
  
 
System Requirements
 
System Requirements
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here we are using
+
|Here we are using
  
 
* '''Ubuntu '''version 12.04
 
* '''Ubuntu '''version 12.04
 
* '''Ruby'''1.9.3
 
* '''Ruby'''1.9.3
 
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 4
+
|Slide 4
  
 
Pre-requisites
 
Pre-requisites
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| To follow this tutorial, you must have '''Internet '''Connection.
+
|To follow this tutorial, you must have knowledge of '''Linux '''commands, '''Terminal '''and '''Text-editor.'''
 
+
 
+
You must also have knowledge of '''Linux '''commands, '''Terminal '''and '''Text-editor.'''
+
 
+
  
 
If not, for relevant tutorials, please visit our website.
 
If not, for relevant tutorials, please visit our website.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Before we begin, recall that we had created “'''ttt'''” directory earlier.
+
|Before we begin, recall that we had created a “'''ttt'''” directory earlier.
 
+
  
 
Let's go to that directory.
 
Let's go to that directory.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the '''terminal''' which has all the commands for creating the directories and the prompt should be in '''classes '''directory
+
|Switch to the '''terminal''' which has all the commands for creating the directories and the prompt should be in '''classes '''directory
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then to '''ruby-tutorial '''and '''classes''' directory.
+
|To '''ruby-tutorial '''and '''classes''' directory.
 
+
 
+
 
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 5
+
|Slide 5
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Ruby''' is an object oriented language.  
+
|'''Ruby''' is an '''object oriented''' language.  
  
 
Everything in '''Ruby''' is an object; from a value to a '''string''' or number.  
 
Everything in '''Ruby''' is an object; from a value to a '''string''' or number.  
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 6 – What is a Class in Ruby?
+
|Slide 6 – What is a Class in Ruby?
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| A '''class''' is a collection of related '''data''' and '''functions'''.
+
|A '''class''' is a collection of related '''data''' and '''functions'''.
  
 
It can serve to keep information organized.
 
It can serve to keep information organized.
Line 78: Line 65:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 7
+
|Slide 7
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| A '''class''' definition begins with the keyword '''class.'''
+
|A '''class''' definition begins with the keyword '''class.'''
  
 
It is followed by the name of the '''class.'''
 
It is followed by the name of the '''class.'''
Line 86: Line 73:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us look at an example of a '''class.'''
+
|Let us look at an example of a '''class.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 8
+
|Slide 8
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''class Product'''
+
|'''class Product'''
  
 
'''ruby code'''
 
'''ruby code'''
Line 100: Line 87:
 
The name of the '''class''' must begin with a capital letter.
 
The name of the '''class''' must begin with a capital letter.
  
Names that contain more than one word should be separated by an '''“underscore (_)”.'''
+
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'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to '''gedit''' where you have already opened the file '''“class_definition.rb” '''with the class defnition''' '''code typed inside.
+
|Switch to '''gedit''' where you have already opened the file '''“class_definition.rb” '''with the class defnition''' '''code typed inside.
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Create a new file in '''gedit '''as shown in the basic level '''Ruby '''tutorials.
+
|Create a new file in '''gedit '''as shown in the basic level '''Ruby '''tutorials.
  
  
Line 110: Line 106:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I have a working example of the implementing '''classes'''.
+
|I have a working example of the implementation of '''classes'''.
  
  
Line 117: Line 113:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''“Order”'''
+
|Highlight '''“Order”'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I have defined a''' class '''named''' Order''' in this example.
+
|I have defined a''' class '''named''' Order''' in this example.
  
 
Now let us make the '''class '''useful by adding some '''variables'''.
 
Now let us make the '''class '''useful by adding some '''variables'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| HIghlight '''“@myinstance”'''
+
|HIghlight '''“@myinstance”'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then I have defined an '''instance variable “myinstance”.'''
+
|Then I have defined an '''instance variable “myinstance”.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the value
+
|Highlight the value
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I have assigned a value to it.
+
|And I have assigned a value to it.
 
+
 
+
 
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''“@myclassvar”'''
+
|Highlight '''“@myclassvar”'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I have also defined a '''class variable “myclassvar”'''
+
|I have also defined a '''class variable “myclassvar”'''
  
 
And I have assigned a value to it.
 
And I have assigned a value to it.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| On the '''terminal''' type
+
|On the '''terminal''' type
  
 
'''Order.instance_variables'''
 
'''Order.instance_variables'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| 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. '''
+
|Now let us add some code that will make use of this class.  
  
'''Before this line, “puts” some characters and slash n for a new line.'''
+
Type''' puts Order dot instance underscore variables. '''
  
'''This is done for readability. '''
 
  
'''Let us copy and paste that and add it just below the line we added and save it.'''
+
|-
 +
|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.'''
 
  
'''Switch to the terminal and type ruby class_definition.rb and see the output.'''
+
|-
 +
|
 +
|Now let us execute this code.
  
 +
|-
 +
|On the '''terminal''', type
  
This is to see the''' instance variables '''of the class '''“Order”.'''
+
'''ruby class_definition.rb'''
 +
|Switch to the '''terminal''' and type
  
|-
+
'''ruby space class underscore definition dot rb'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Point to the output
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Inspect the output.
+
  
You will see all the''' instance variable you defineds''' of''' class “Order”.'''
+
and see the output.
  
From that list you can spot the one that you defined in the '''class.'''
+
|-
 +
|Point to the output
 +
|You will see the''' instance variable''' you defined.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Not sure if this is a repetition of the above 2 rows.Next let us type execute''' Order.class_variables .'''
+
|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.'''and inspect the output.''' '''
+
Let us copy and paste the demarcation, just below the line and save it.
  
Now let us switch to the terminal and execute the file like before.
+
|-
 +
|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 will notice the '''class variable '''you defined, also show up.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| You should now be able to write your own''' class.'''
+
|You should now be able to write your own''' class.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Next, let us look at what an '''object '''is.
+
|Next, let us look at what an '''object '''is.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 9 – What is an object?
+
|Slide 9 – What is an object?
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| An''' object''' is an '''instance '''of a '''class.'''
+
|An''' object''' is an '''instance '''of a '''class.'''
  
 
Which means an''' object '''is created from a '''class.'''
 
Which means an''' object '''is created from a '''class.'''
Line 194: Line 197:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 10 – How do you declare an object
+
|Slide 10 – How do you declare an object
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| We declare an '''object''' of a '''class '''using the '''new''' keyword.
+
|How do you declare an '''object'''
 +
 
 +
We declare an '''object''' of a '''class '''using the '''new keyword.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 11
+
|Slide 11
 
+
 
+
  
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here we are declaring an '''object '''of the''' Product class.'''
+
|Here we are declaring an '''object '''of the''' Product class.'''
  
 
Here an '''object '''gets created.
 
Here an '''object '''gets created.
Line 213: Line 216:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 12
+
|Slide 12
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let is look at what an '''“initialize” method '''is.
+
|Now let is look at what an '''“initialize” method '''is.
  
 
An''' initialize method''' is called at the time of '''object''' creation.
 
An''' initialize method''' is called at the time of '''object''' creation.
  
  
In our previous examples we declared''' objects.'''
+
On declaring calling '''new''' on an '''object''', we invoke the''' initialize method.'''
 
+
 
+
On declaringcalling new on an '''object''' we have invoked the''' initialize method.'''
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 13
+
|Slide 13
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| An '''initialize method '''may take a list of parameters.
+
|An '''initialize method '''may take a list of '''parameters'''.
  
  
Like other '''Ruby methods,''' it is preceded by the '''“def” '''keyword.
+
Like other '''Ruby methods,''' it is preceded by the '''“def” keyword.'''
  
  
Line 235: Line 235:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to gedit where you have already opened the file “object_initialize.rb” with the '''object initialize '''code typed inside.
+
|Switch to '''gedit''' where you have already opened the file “object_initialize.rb” with the '''object initialize '''code typed inside.
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Create a new file in '''gedit '''as shown in the basic level '''Ruby '''tutorials.
+
|Create a new file in '''gedit '''as shown in the basic level '''Ruby '''tutorials.
  
  
Line 242: Line 242:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I have a working example of the '''object initialization''' code.
+
|I have a working example of the '''object initialization''' code.
  
  
Line 249: Line 249:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''class Order'''
+
|Highlight '''class Order'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here I have defined a '''class '''called''' “Order”'''
+
|Here I have defined a '''class '''called''' “Order”'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight''' initialize method'''
+
|Highlight''' initialize method'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then I have defined the '''initialize method '''with no '''argument.'''
+
|Then I have defined the '''initialize method, '''with no '''argument.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the''' puts '''declaration
+
|Highlight the''' puts '''declaration
 
+
  
  
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I have defined a '''puts method''' to display the message''' “I have created an object”.'''
+
|I have defined a '''puts method''' to display the message''' “I have created an object”.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Then highlight next line that declares the''' object.'''
+
|Then highlight next line that declares the''' object.'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Next I have defined''' Order.new.'''
+
|Next, I have defined''' Order dot new.'''
  
 
This will invoke the '''initialize method.'''
 
This will invoke the '''initialize method.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| On the '''terminal '''type
+
|On the '''terminal '''type
  
 
'''ruby object_initialize.rb '''
 
'''ruby object_initialize.rb '''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Switch to the '''terminal '''and type
+
|Switch to the '''terminal '''and type
  
'''ruby object underscore initialize dot rb '''
+
'''ruby space object underscore initialize dot rb '''
  
  
Line 281: Line 280:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Point to the output
+
|Point to the output
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| You will see the message''' “I have created an object” '''printed out.
+
|You will see the message''' “I have created an object” '''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to '''gedit.'''
+
|Switch to '''gedit.'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us go back to '''gedit''' and add an '''argument''' to the '''method.'''
+
|Now let us go back to '''gedit''' and add an '''argument''' to the '''method.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the code with''' puts.'''
+
|Highlight the code with''' puts.'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us modify the '''puts '''code.
+
|Let us modify the '''puts '''.
  
 
It should display the value of the '''argument''' passed.
 
It should display the value of the '''argument''' passed.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Visual Cue is missing
+
|Visual Cue is missing
  
 
Highlight the line Order.new
 
Highlight the line Order.new
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Next let us call''' type '''
+
|Next let us type
  
'''Order.new(“I have initialized youcreated an object”).'''
+
'''Order dot new(“I have created an object”).'''
  
 
Here we have given an '''argument '''to the''' new method.'''
 
Here we have given an '''argument '''to the''' new method.'''
  
 
This''' argument''' gets passed on to the''' initialize method.'''
 
This''' argument''' gets passed on to the''' initialize method.'''
 
'''Next let us replace the string with “arg”.'''
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Switch to the '''terminal '''and type
+
|Switch to the '''terminal '''and type
  
'''ruby object_initialize.rb''' and see the output.
+
'''ruby space object underscore initialize dot rb'''  
 +
 
 +
and see the output.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| You will see''' '''the message '''“I have initialized youcreated an object”''' printed out.
+
|You will see the message '''“I created an object”''' printed out.
  
 
Now, you would have understood what '''object initialization '''means.
 
Now, you would have understood what '''object initialization '''means.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 14 – Defining methods in a class
+
|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” “end” '''block.
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Recall that in '''Ruby, methods '''are the '''functions '''that a''' class''' performs.
+
  
  
Each '''method '''in a '''class '''is defined within the''' “def” “end” '''block.
+
Each '''method '''in a '''class '''is defined within the''' “def” and “end” '''block.
  
  
Line 334: Line 330:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 154 – Defining methods in a class
+
|Slide 15 – Defining methods in a class
  
 
'''contd...'''
 
'''contd...'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Some of the characters that can be appended to a '''method '''name are:
+
|Some of the characters that can be appended to a '''method '''name are:
  
* '''?'''
+
* '''?''' (question-mark)
* '''<nowiki>=</nowiki>'''
+
* '''=''' (equal to)
  
 
Each of the characters add some meaning to the '''method.'''
 
Each of the characters add some meaning to the '''method.'''
Line 347: Line 343:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to gedit.
+
|Switch to gedit.
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Create a new file in '''gedit '''as shown in the basic level '''Ruby '''tutorials.
+
|Create a new file in '''gedit '''as shown in the basic level '''Ruby '''tutorials.
  
  
And name it '''class_methods.rb'''
+
And name it '''class underscore methods dot rb'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I have a working example of '''class methods''' code.
+
|I have a working example of '''class methods''' code.
  
  
Line 361: Line 357:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''class Animal'''
+
|Highlight '''class Animal'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here I have defined a''' class '''named''' “Animal”.'''
+
|Here I have defined a''' class '''named''' “Animal”.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Then highlight '''methods breathe '''and''' walk'''
+
|Then highlight '''methods breathe '''and''' walk'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then I have two '''methods “breathe” '''and''' “walk”.'''
+
|Then I have two '''methods “breathe” '''and''' “walk”.'''
  
 
They are both defined with the '''“def” '''and''' “end” '''keywords.
 
They are both defined with the '''“def” '''and''' “end” '''keywords.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''Animal.new'''
+
|Highlight '''Animal.new'''
  
 
Then highlight '''animal.breathe''' and then
 
Then highlight '''animal.breathe''' and then
  
 
'''animal.walk'''
 
'''animal.walk'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I have then initialized the '''object Animal.'''
+
|I have then initialized the '''object Animal.'''
  
I have assigned it to a variable called''' “animal” with lowercase “a”.'''
+
I have assigned it to a variable called''' “animal”''' with lowercase '''“a”.'''
  
 
Then I have invoked the '''methods “breathe” '''and''' “walk” '''consequtively.
 
Then I have invoked the '''methods “breathe” '''and''' “walk” '''consequtively.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| On the '''terminal '''type
+
|On the '''terminal '''type
  
 
'''ruby class_methods.rb'''
 
'''ruby class_methods.rb'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us execute the program.
+
|Now let us execute the program.
  
  
Line 397: Line 393:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output.
+
|Highlight the output.
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| You will notice the lines:
+
|You will notice the lines:
  
 
'''“ I breathe”'''
 
'''“ I breathe”'''
Line 406: Line 402:
 
printed out.
 
printed out.
  
This is because you have invoked the''' '''two''' methods “breathe” '''and '''“walk”.'''
+
This is because you have invoked the two''' methods “breathe” '''and '''“walk”.'''
 
+
These '''methods '''are invoked on lines 15 and 16 respectively.
+
  
  
Line 414: Line 408:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Next let us look at how to create '''methods '''with a trailing '''question mark.'''
+
|Next, let us look at how to create '''methods '''with a trailing '''question mark.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to''' gedit.'''
+
|Switch to''' gedit.'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Create a new file in '''gedit '''as shown in the basic level '''Ruby '''tutorials.
+
|Create a new file in '''gedit '''as shown in the basic level '''Ruby '''tutorials.
  
  
Line 425: Line 419:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| In '''gedit'''
+
|In '''gedit'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I have a working example of '''class methods '''with '''question mark''' code.
+
|I have a working example of '''class methods '''with '''question mark''' code.
  
  
Line 432: Line 426:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight''' “breathe?” '''line
+
|Highlight''' “breathe?” '''line
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here I have taken the same '''class''' as before as an example.
+
|Here I have taken the same '''class''' as before as an example.
  
 
Here the '''breathe method''' has a trailing '''“question mark (?)”.'''
 
Here the '''breathe method''' has a trailing '''“question mark (?)”.'''
Line 440: Line 434:
  
  
This is based on the '''method '''naming''' '''convention of '''Ruby.'''
+
This is based on the '''method '''naming convention''' of '''Ruby.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight''' animal.breathe?'''
+
|Highlight''' animal.breathe?'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The '''method''' gets invoked by declaring''' animal.breathe?'''
+
|The '''method''' gets invoked by declaring''' animal dot breathe question-mark'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| On the '''terminal '''type
+
|On the '''terminal '''type
  
 
'''ruby class_methods_with_trailing_characters.rb '''
 
'''ruby class_methods_with_trailing_characters.rb '''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Switch to the '''terminal '''and type
+
|Switch to the '''terminal '''and type
  
'''ruby class underscore methods underscore with underscore trailing underscore characters dot rb'''
+
'''ruby space class underscore methods underscore with underscore trailing underscore characters dot rb'''
  
  
Line 458: Line 452:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| You will notice the output as :
+
|You will notice the output as :
  
 
'''“true”'''
 
'''“true”'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight''' “walk=(value)” method'''
+
|Highlight''' “walk=(value)” method'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Next let us define another '''method '''called''' “walk”.'''
+
|Next, let us define another '''method '''called''' “walk”.'''
  
 
Let us place an '''equal-to '''sign '''(“=(value)”) '''beside it.
 
Let us place an '''equal-to '''sign '''(“=(value)”) '''beside it.
  
The let us execute this '''method.'''
+
Llet us invoke this '''method''' by calling '''animal dot walk'''.
 +
 
 +
 
 +
Then let us execute this '''method'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| On the '''terminal '''type
+
|On the '''terminal '''type
  
 
'''ruby class_methods_with_trailing_characters.rb '''
 
'''ruby class_methods_with_trailing_characters.rb '''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Switch to the '''terminal '''and type
+
|Switch to the '''terminal '''and type
  
 
'''ruby class underscore methods underscore with underscore trailing underscore characters dot rb'''
 
'''ruby class underscore methods underscore with underscore trailing underscore characters dot rb'''
Line 483: Line 480:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the error
+
|Highlight the error
  
  
 
+
|It will give an '''“undefined method”''' error.
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| It will give an '''“undefined method”''' error.
+
  
  
Line 493: Line 489:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Type the code
+
|Type the code
  
 
'''animal.walk = “ hops”'''
 
'''animal.walk = “ hops”'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| It is used to assign a value to the '''method.'''
+
|It is used to assign a value to a'''method.'''
  
 
So, let us invoke the '''method '''a little differently this time.
 
So, let us invoke the '''method '''a little differently this time.
  
  
'''animal.walk = “ hops”'''
+
Type '''puts animal dot walk equal to “ hops”'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let give it another try.
+
|Now let give it another try.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| On the '''terminal '''type
+
|On the '''terminal '''type
  
 
'''ruby class_methods_with_trailing_characters.rb '''
 
'''ruby class_methods_with_trailing_characters.rb '''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Switch to the '''terminal '''and type
+
|Switch to the '''terminal '''and run the command like before and see the output.
 
+
'''ruby class underscore methods underscore with underscore trailing underscore characters dot rbrub the command like before.'''
+
 
+
 
+
and see the output.
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| On the '''terminal '''
+
|On the '''terminal '''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| You will notice that the word '''“hops”''' get printed.
+
|You will notice that the word '''“hops”''' get printed.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This demonstrates that the''' equal to sign''' next to a '''method''' means '''assignment.'''
+
|This demonstrates that the ''' equal to sign''' next to a '''method''' means '''assignment.'''
  
 
Now you should be able to write your own''' methods.'''
 
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.'''<nowiki><PAUSE></nowiki>'''
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 15 – Defining accessor methods in a class
+
|Slide 16
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| There are three ways of defining '''accessor methods''' in a '''class'''
+
 
+
* '''attr_reader'''
+
* '''attr_writer'''
+
 
+
'''attr_accessor '''
+
 
+
|-
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 15 contd...
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| * 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.
+
 
+
 
+
 
+
|-
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to '''gedit'''
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Create a new file in gedit as shown in the basic level Ruby tutorials.'''
+
 
+
 
+
And name it '''class underscore attribute underscore accessors dot rb'''
+
 
+
|-
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the '''class definition.'''
+
 
+
Highlight the next line that has '''attr_reader'''
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| 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.'''
+
 
+
|-
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the''' object initialization '''line.
+
 
+
Then highlight the '''puts '''line.
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| 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.
+
 
+
|-
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''On the terminal type'''
+
 
+
'''ruby class_attribute_accessors.rb '''
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Switch to the terminal and type'''
+
 
+
'''ruby class underscore attribute underscore accessors dot rb '''
+
 
+
and see the output.
+
 
+
|-
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Point to the '''terminal'''
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| There will be no output.
+
 
+
This is because the''' “name” attribute''' does not have any value assigned to it.
+
 
+
|-
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Back on '''gedit'''
+
 
+
Type the code
+
 
+
'''product.name = “ipad”'''
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| 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.
+
 
+
|-
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the '''puts “The name of the product is” '''part of the code.
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us modify the '''“puts” '''so that the sentence is more meaningful.
+
 
+
|-
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"|
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us execute the code and inspect the output.
+
 
+
|-
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Point to the output
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The output will say '''“The name of the product is ipad”.'''
+
 
+
|-
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"|
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Next let us define an '''attr_accessor''' for''' “price”.'''
+
 
+
|-
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Visual Cue missing
+
 
+
Is this a slide or will you point to some code
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| 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
+
 
+
|-
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Type the code.
+
 
+
'''puts “The price of the product is: “+ product.price.to_s'''
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| 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'''
+
 
+
|-
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''On the terminal type'''
+
 
+
'''ruby class_attribute_accessors.rb '''
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Switch to the terminal and type'''
+
 
+
'''ruby class underscore attribute underscore accessors dot rb '''
+
 
+
and see the output.
+
 
+
|-
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"|
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| You will notice the string declared with''' “puts” '''gets displayed.
+
 
+
|-
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"|
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now you should be able to write your own '''attribute accessors, readers and writers.'''
+
 
+
|-
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide 16 -- Assignment'''
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| 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}”'''
+
 
+
|-
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 16
+
  
 
Summary
 
Summary
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| In this tutorial we have learnt-
+
|In this tutorial we have learnt-
 
+
* '''How to declare classes'''
+
* '''How to create objects of a class'''
+
* '''Different ways of defining methods in ruby'''
+
 
+
  
 +
* How to declare '''classes'''
 +
* How to create '''objects''' of a '''class'''
 +
* Different ways of defining '''methods''' in '''Ruby'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 17
+
|Slide 17
  
 
Assingment
 
Assingment
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| As an assignment:
+
|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.
+
 
+
  
 +
* 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.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 182
+
|Slide 18
  
 
About the Spoken Tutorial Project
 
About the Spoken Tutorial Project
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Watch the video available at the following link.
+
|Watch the video available at the following link.
  
 
It summarizes the Spoken Tutorial project.
 
It summarizes the Spoken Tutorial project.
Line 722: Line 551:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 193
+
|Slide 193
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team :
+
|The Spoken Tutorial Project Team :
  
  
Line 735: Line 564:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide 2014
+
|Slide 2014
  
 
Acknowledgements
 
Acknowledgements
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is a part of the Talk to a Teacher project.
+
|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.
 
It is supported by the National Mission on Education through ICT, MHRD, Government of India.
Line 747: Line 576:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Previous slide
+
|Previous slide
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This is Anjana Nair signing off. Thank you
+
|This is Anjana Nair signing off. Thank you
  
 
|}
 
|}

Latest revision as of 19:30, 16 March 2014

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