Difference between revisions of "Ruby/C2/Variables-in-Ruby/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 2: Line 2:
  
 
'''Author: Afrin pinjari'''
 
'''Author: Afrin pinjari'''
 
  
 
'''Keywords: to_s, to_i, video tutorial, local variable, global variable'''
 
'''Keywords: to_s, to_i, video tutorial, local variable, global variable'''
  
  
 
+
{| border=1
{| style="border-spacing:0;"
+
| '''Visual Cue'''
! <center>Visual Clue</center>
+
| '''Narration'''
! <center>Narration</center>
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 1
+
|Slide 1
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| <center>Welcome to the Spoken Tutorial on </center>
+
|Welcome to the Spoken Tutorial on Variables in '''Ruby'''
 
+
<center>Variables in '''Ruby'''</center>
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 2
+
|Slide 2
  
 
Learning Objectives
 
Learning Objectives
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn
+
|In this tutorial we will learn
  
 
* What is a variable?
 
* What is a variable?
Line 30: Line 26:
 
* What is variable's scope?
 
* What is variable's scope?
 
* Types of variables
 
* Types of variables
 
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 3
+
|Slide 3
  
 
System Requirements
 
System Requirements
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we are using  
+
|Here we are using  
  
 
* '''Ubuntu''' Linux version 12.04  
 
* '''Ubuntu''' Linux version 12.04  
 
* '''Ruby''' 1.9.3
 
* '''Ruby''' 1.9.3
 
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 4
+
|Slide 4
  
 
Pre-requisites
 
Pre-requisites
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To follow this tutorial you must have knowledge of using''' Terminal''' in '''Linux.'''
+
|To follow this tutorial you must have knowledge of using''' Terminal''' in '''Linux.'''
 
+
  
 
You must also be familiar with '''irb'''
 
You must also be familiar with '''irb'''
 
  
 
If not, for relevant tutorials, please visit our website
 
If not, for relevant tutorials, please visit our website
 
 
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 5
+
|Slide 5
  
 
Variables
 
Variables
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now I will explain what a variable is.
+
|Now I will explain what a variable is.
 
+
  
 
* Variable is used to store a value.
 
* Variable is used to store a value.
 
* Variable is a reference that can be assigned.
 
* Variable is a reference that can be assigned.
 
* Please note that '''Ruby '''variables are''' case sensitive.'''
 
* Please note that '''Ruby '''variables are''' case sensitive.'''
 
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 6
+
|Slide 6
  
 
Variables
 
Variables
  
 
+
|* Variable names should be '''meaningful'''.  
 
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| * Variable names should be '''meaningful'''.  
+
 
* Variable name may only contain lowercase letters, numbers and underscores.  
 
* Variable name may only contain lowercase letters, numbers and underscores.  
 
* Ex : first_name
 
* Ex : first_name
 
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 7
+
|Slide 7
  
 
Dynamic Typing  
 
Dynamic Typing  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now''' '''let us see what is dynamic typing
+
|Now''' '''let us see what is dynamic typing
  
 
* '''Ruby''' is a dynamic typed language.
 
* '''Ruby''' is a dynamic typed language.
 
* It means that you '''don't''' need to declare '''datatype''' while creating a variable.
 
* It means that you '''don't''' need to declare '''datatype''' while creating a variable.
 
 
* '''Ruby interpreter '''determines the data type at the time of assignment.
 
* '''Ruby interpreter '''determines the data type at the time of assignment.
 
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us see how to declare a variable in '''Ruby.'''
+
|Now let us see how to declare a variable in '''Ruby.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Ctrl+Alt+t'''
+
|Press '''Ctrl+Alt+t'''
 
+
  
 
Switch to the terminal
 
Switch to the terminal
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Open the terminal by pressing '''Ctrl,Alt''' and''' T '''keys simultaneously.
+
|Open the terminal by pressing '''Ctrl,Alt''' and''' T '''keys simultaneously.
 
+
  
 
A terminal window appears on your screen.
 
A terminal window appears on your screen.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
|Type
  
 
'''irb'''<nowiki><<Press </nowiki>'''Enter'''
 
'''irb'''<nowiki><<Press </nowiki>'''Enter'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type''' '''
+
|Type '''irb'''
 
+
'''irb'''
+
  
 
Press '''Enter '''to launch '''Interactive Ruby'''
 
Press '''Enter '''to launch '''Interactive Ruby'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type  
+
|Type  
  
 
'''var1=10 '''<nowiki><<press </nowiki>'''Enter'''
 
'''var1=10 '''<nowiki><<press </nowiki>'''Enter'''
  
 
+
|Now type '''var1''' equal to '''10 '''
 
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now type  
+
 
+
'''var1''' equal to '''10 '''
+
  
 
Press '''Enter'''
 
Press '''Enter'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''var1'''
+
|Highlight '''var1'''
  
 
Highlight '''10'''
 
Highlight '''10'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have declared a variable '''var1''' and assigned a value '''10 '''to it'''.'''
+
|Here we have declared a variable '''var1''' and assigned a value '''10 '''to it'''.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let's check whether the datatype allotted by the interpreter is  
+
|Let's check whether the datatype allotted by the interpreter is '''integer '''or not.
 
+
'''integer '''or not''' . '''
+
 
+
 
+
 
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
|Type
  
 
'''var1.kind_of?Integer '''<nowiki><< press </nowiki>'''Enter'''
 
'''var1.kind_of?Integer '''<nowiki><< press </nowiki>'''Enter'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| So, type  
+
|So, type  
  
 
'''var1 '''dot '''kind''' underscore '''of '''question mark '''Integer'''
 
'''var1 '''dot '''kind''' underscore '''of '''question mark '''Integer'''
Line 161: Line 125:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''true'''
+
|Highlight '''true'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We will get the output as '''true.'''
+
|We will get the output as '''true.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In''' Ruby''' you can dynamically change the variable type.
+
|In''' Ruby''' you can dynamically change the variable type.
  
 
To do so, just assign a new value to it.
 
To do so, just assign a new value to it.
Line 173: Line 137:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type  
+
|Type  
  
 
'''var1=”hello”'''<nowiki><< press </nowiki>'''Enter'''
 
'''var1=”hello”'''<nowiki><< press </nowiki>'''Enter'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type
+
|Type
  
 
'''var1''' equal to within double quote '''hello'''  
 
'''var1''' equal to within double quote '''hello'''  
  
press Enter
+
press '''Enter'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
|Type
  
 
'''<nowiki>var1.class<< </nowiki>'''press '''Enter'''
 
'''<nowiki>var1.class<< </nowiki>'''press '''Enter'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let's verify the variable type assigned  
+
|Let's verify the variable type assigned  
 
+
Type
+
 
+
'''var1''' dot '''class '''
+
  
 +
Type '''var1''' dot '''class '''
  
 +
|-
 +
|Highlight''' .class'''
 +
|Class method tells us what class of variable it is and
  
 +
Now Press''' Enter'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight''' .class'''
+
|Highlight '''string'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Class method tells us what class of variable it is and
+
|We get the output as '''string'''
 
+
Now Press''' Enter'''
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''string'''
+
|
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We get the output as '''string'''
+
|'''Ruby '''has automatically changed the variable type from '''integer''' to '''string'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Ruby '''has automatically changed the variable type from '''integer''' to '''string'''
+
||We will now learn how to convert a variable value to a different type.
 +
 
 +
Let's switch back to the slides
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 7
+
|Slide 7
  
 
Converting variable types
 
Converting variable types
 
  
 
Highlight '''to_s'''
 
Highlight '''to_s'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We will now learn how to convert a variable value to different type
 
 
Lets switch back to slides
 
 
  
'''Ruby''' variable classes have methods to convert their value to a different type
+
|'''Ruby''' variable classes have methods to convert their value to a different type
  
 
* '''to_i '''method is used to convert a variable to '''integer'''
 
* '''to_i '''method is used to convert a variable to '''integer'''
Line 233: Line 193:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Press Ctrl+L'''
+
|Press '''Ctrl+L'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us try out these methods.
+
|Now let us try out these methods.
  
 
Go to the''' terminal'''  
 
Go to the''' terminal'''  
  
Lets clear the terminal first
+
Let's clear the '''terminal''' first.
 
+
Press Ctrl,l to clear the '''irb '''console
+
 
+
 
+
  
 +
Press '''Ctrl + L''' to clear the '''irb '''console
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
|Type
  
'''<nowiki>y=20<<</nowiki>'''press''' '''Enter
+
'''<nowiki>y=20<<</nowiki>'''press''' Enter'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now Type
+
|Now type '''y '''equal to '''20''' and press''' Enter '''
 
+
'''y '''equal to '''20'''
+
 
+
and Press''' '''Enter
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''y''' and '''20'''
+
|Highlight '''y''' and '''20'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have declared a variable called''' y''' and assigned a value '''20''' to it.
+
|Here we have declared a variable called''' y''' and assigned a value '''20''' to it.
  
 
We will now convert '''y''' to a floating point value using '''to '''underscore''' f''' method
 
We will now convert '''y''' to a floating point value using '''to '''underscore''' f''' method
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
|Type
  
 
'''<nowiki>y.to_f<< </nowiki>'''press''' Enter'''
 
'''<nowiki>y.to_f<< </nowiki>'''press''' Enter'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type
+
|Type '''y '''dot '''to '''underscore '''f''' and press '''Enter'''
 
+
'''y '''dot '''to '''underscore '''f''' and
+
 
+
Press '''Enter'''
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight''' 20.0'''
+
|Highlight''' 20.0'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We will get the value as float'''.'''
+
|We will get the value as '''float'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type  
+
|Type  
  
 
'''<nowiki>y.to_s<<</nowiki>''' press''' Enter'''
 
'''<nowiki>y.to_s<<</nowiki>''' press''' Enter'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now Type
+
|Now type '''y '''dot '''to '''underscore '''s'''and press '''Enter'''
 
+
'''y '''dot '''to '''underscore '''s.'''and
+
 
+
Press '''Enter'''
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''“20”'''
+
|Highlight '''“20”'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We will get output '''20 '''within double quotes
+
|We will get output '''20 '''within double quotes
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To convert variable '''y''' in binary form give number base as '''2''' in '''to_s''' method
+
|To convert variable '''y''' in binary form give number base as '''2''' in '''to_s''' method
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Up Arrow'''
+
|Press '''Up Arrow'''
  
 
+
|Press up arrow key to get the previous command  
 
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Press up arrow key to get the previous command  
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type  
+
|Type '''y.to_s<nowiki>(2)<<</nowiki>'''press''' Enter'''
 
+
|Now type '''opening bracket 2 closing bracket''' and press '''Enter'''
y.to_s'''<nowiki>(2)<<</nowiki>'''press''' Enter'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now type
+
 
+
opening bracket 2 closing bracket and  
+
 
+
press '''Enter'''
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''10100'''
+
|Highlight '''10100'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We will get the output''' 10100 '''in the  binary form
+
|We will get the output''' 10100 '''in the  binary form
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Similarly you can convert variable '''y''' to '''octal''' or '''hexadecimal''' form  
+
|Similarly you can convert variable '''y''' to '''octal''' or '''hexadecimal''' form  
  
 
By changing the number base to 8 or 16.
 
By changing the number base to 8 or 16.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us switch back to our slide
+
|Let us switch back to our slide
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 8
+
|Slide 8
 
+
  
 
Variable Scope
 
Variable Scope
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We will now learn what is '''variable scope'''.
+
|We will now learn what is '''variable scope'''.
 
+
  
 
'''Scope''' defines where in a program a variable is accessible.
 
'''Scope''' defines where in a program a variable is accessible.
 
  
 
'''Ruby''' has four types of variable scope:
 
'''Ruby''' has four types of variable scope:
Line 341: Line 275:
 
* '''Instance '''
 
* '''Instance '''
 
* '''Class '''
 
* '''Class '''
 
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 9
+
|Slide 9
  
 
Naming Convention
 
Naming Convention
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Each variable type is declared by using a special character  
+
|Each variable type is declared by using a special character  
  
 
At the beginning of the variable name  
 
At the beginning of the variable name  
 
  
 
Name Beginning With Variable Scope  
 
Name Beginning With Variable Scope  
Line 375: Line 306:
  
 
Upper case letters represents a '''constant'''
 
Upper case letters represents a '''constant'''
 
 
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We will learn in detail about this in another tutorial.
+
|We will learn in detail about this in another tutorial.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| <nowiki><<Pause>></nowiki>
+
|<nowiki><<Pause>></nowiki>
  
 
This brings us to the end of this Spoken Tutorial.let us summarise
 
This brings us to the end of this Spoken Tutorial.let us summarise
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 11
+
|Slide 11
 
+
  
 
Summary
 
Summary
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we have learnt
+
|In this tutorial we have learnt
  
 
* To declare a variable <br/> eg var1=10
 
* To declare a variable <br/> eg var1=10
 
* Changing variable type using to_f, to_s methods
 
* Changing variable type using to_f, to_s methods
 
* Different Variable '''scope'''
 
* Different Variable '''scope'''
 
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 13
+
|Slide 13
  
 
Assignment
 
Assignment
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As an assignment
+
|As an assignment
  
 
Declare a variable and convert it to '''octal''' and '''hexadecimal''' form
 
Declare a variable and convert it to '''octal''' and '''hexadecimal''' form
 
 
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 14
+
|Slide 14
  
 
About the Spoken Tutorial Project
 
About the Spoken Tutorial Project
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Watch the video available at the following link.
+
|Watch the video available at the following link.
 
+
  
 
It summarises the Spoken Tutorial project.
 
It summarises the Spoken Tutorial project.
Line 425: Line 346:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 15
+
|Slide 15
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team :
+
|The Spoken Tutorial Project Team :
 
+
  
 
Conducts workshops using spoken tutorials  
 
Conducts workshops using spoken tutorials  
Line 438: Line 358:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 16
+
|Slide 16
  
  
 
Acknowledgement
 
Acknowledgement
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt 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.

Latest revision as of 19:33, 18 August 2013

Title of script: Variables in Ruby

Author: Afrin pinjari

Keywords: to_s, to_i, video tutorial, local variable, global variable


Visual Cue Narration
Slide 1 Welcome to the Spoken Tutorial on Variables in Ruby
Slide 2

Learning Objectives

In this tutorial we will learn
  • What is a variable?
  • Dynamic typing in Ruby
  • Declaring a variable
  • Converting variable types
  • What is variable's scope?
  • Types of variables
Slide 3

System Requirements

Here we are using
  • Ubuntu Linux version 12.04
  • Ruby 1.9.3
Slide 4

Pre-requisites

To follow this tutorial you must have knowledge of using Terminal in Linux.

You must also be familiar with irb

If not, for relevant tutorials, please visit our website

Slide 5

Variables

Now I will explain what a variable is.
  • Variable is used to store a value.
  • Variable is a reference that can be assigned.
  • Please note that Ruby variables are case sensitive.
Slide 6

Variables

* Variable names should be meaningful.
  • Variable name may only contain lowercase letters, numbers and underscores.
  • Ex : first_name
Slide 7

Dynamic Typing

Now let us see what is dynamic typing
  • Ruby is a dynamic typed language.
  • It means that you don't need to declare datatype while creating a variable.
  • Ruby interpreter determines the data type at the time of assignment.
Now let us see how to declare a variable in Ruby.
Press Ctrl+Alt+t

Switch to the terminal

Open the terminal by pressing Ctrl,Alt and T keys simultaneously.

A terminal window appears on your screen.

Type

irb<<Press Enter

Type irb

Press Enter to launch Interactive Ruby

Type

var1=10 <<press Enter

Now type var1 equal to 10

Press Enter

Highlight var1

Highlight 10

Here we have declared a variable var1 and assigned a value 10 to it.
Let's check whether the datatype allotted by the interpreter is integer or not.
Type

var1.kind_of?Integer << press Enter

So, type

var1 dot kind underscore of question mark Integer

and press Enter

Highlight true We will get the output as true.
In Ruby you can dynamically change the variable type.

To do so, just assign a new value to it.

Let's do this by assigning a string value to variable var1.

Type

var1=”hello”<< press Enter

Type

var1 equal to within double quote hello

press Enter

Type

var1.class<< press Enter

Let's verify the variable type assigned

Type var1 dot class

Highlight .class Class method tells us what class of variable it is and

Now Press Enter

Highlight string We get the output as string
Ruby has automatically changed the variable type from integer to string
We will now learn how to convert a variable value to a different type.

Let's switch back to the slides

Slide 7

Converting variable types

Highlight to_s

Ruby variable classes have methods to convert their value to a different type
  • to_i method is used to convert a variable to integer
  • to_f method is used to convert a variable to floating point value
  • to_s method is used to convert a variable to string


The to _ s() method takes number base as an argument.

The conversion depends on this number base.

Press Ctrl+L Now let us try out these methods.

Go to the terminal

Let's clear the terminal first.

Press Ctrl + L to clear the irb console

Type

y=20<<press Enter

Now type y equal to 20 and press Enter
Highlight y and 20 Here we have declared a variable called y and assigned a value 20 to it.

We will now convert y to a floating point value using to underscore f method

Type

y.to_f<< press Enter

Type y dot to underscore f and press Enter
Highlight 20.0 We will get the value as float.
Type

y.to_s<< press Enter

Now type y dot to underscore sand press Enter
Highlight “20” We will get output 20 within double quotes
To convert variable y in binary form give number base as 2 in to_s method
Press Up Arrow Press up arrow key to get the previous command
Type y.to_s(2)<<press Enter Now type opening bracket 2 closing bracket and press Enter
Highlight 10100 We will get the output 10100 in the binary form
Similarly you can convert variable y to octal or hexadecimal form

By changing the number base to 8 or 16.

Let us switch back to our slide
Slide 8

Variable Scope

We will now learn what is variable scope.

Scope defines where in a program a variable is accessible.

Ruby has four types of variable scope:

  • Local
  • Global
  • Instance
  • Class
Slide 9

Naming Convention

Each variable type is declared by using a special character

At the beginning of the variable name

Name Beginning With Variable Scope

$ A global variable

[a-z] or _ A local variable

@ An instance variable

@@ A class variable

[A-Z] A constant


$ represents global variable

Lower case letters and underscore represents a local variable

@ represents an instance variable

Two @@ represents a class variable

Upper case letters represents a constant

We will learn in detail about this in another tutorial.
<<Pause>>

This brings us to the end of this Spoken Tutorial.let us summarise

Slide 11

Summary

In this tutorial we have learnt
  • To declare a variable
    eg var1=10
  • Changing variable type using to_f, to_s methods
  • Different Variable scope
Slide 13

Assignment

As an assignment

Declare a variable and convert it to octal and hexadecimal form

Slide 14

About the Spoken Tutorial Project

Watch the video available at the following link.

It summarises the Spoken Tutorial project.

If you do not have good bandwidth, you can download and watch it.

Slide 15 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 16


Acknowledgement

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.

This is Afrin Pinjari from IIT Bombay, signing off.

Thank you for watching.

Contributors and Content Editors

Afrin, Nancyvarkey, Sneha