Difference between revisions of "Ruby/C2/Variables-in-Ruby/English"
(Created page with ''''Title of script''': Variables in Ruby '''Author: Afrin pinjari''' '''Keywords: to_s, to_i, video tutorial, local variable, global variable''' {| style="border-spacing:0;…') |
Nancyvarkey (Talk | contribs) |
||
(One intermediate revision by one other user not shown) | |||
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 | |
− | {| | + | | '''Visual Cue''' |
− | + | | '''Narration''' | |
− | + | ||
|- | |- | ||
− | + | |Slide 1 | |
− | | | + | |Welcome to the Spoken Tutorial on Variables in '''Ruby''' |
− | + | ||
− | + | ||
|- | |- | ||
− | + | |Slide 2 | |
Learning Objectives | Learning Objectives | ||
− | + | |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 | ||
− | |||
− | |||
|- | |- | ||
− | + | |Slide 3 | |
System Requirements | System Requirements | ||
− | + | |Here we are using | |
* '''Ubuntu''' Linux version 12.04 | * '''Ubuntu''' Linux version 12.04 | ||
* '''Ruby''' 1.9.3 | * '''Ruby''' 1.9.3 | ||
− | |||
− | |||
|- | |- | ||
− | + | |Slide 4 | |
Pre-requisites | Pre-requisites | ||
− | + | |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 | ||
− | |||
− | |||
− | |||
|- | |- | ||
− | + | |Slide 5 | |
Variables | Variables | ||
− | + | |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.''' | ||
− | |||
− | |||
|- | |- | ||
− | + | |Slide 6 | |
Variables | Variables | ||
− | + | |* 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 | ||
− | |||
− | |||
|- | |- | ||
− | + | |Slide 7 | |
Dynamic Typing | 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. | ||
− | |||
− | |||
|- | |- | ||
− | + | | | |
− | + | |Now let us see how to declare a variable in '''Ruby.''' | |
|- | |- | ||
− | + | |Press '''Ctrl+Alt+t''' | |
− | + | ||
Switch to the terminal | Switch to the terminal | ||
− | + | |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. | ||
|- | |- | ||
− | + | |Type | |
'''irb'''<nowiki><<Press </nowiki>'''Enter''' | '''irb'''<nowiki><<Press </nowiki>'''Enter''' | ||
− | + | |Type '''irb''' | |
− | + | ||
− | '''irb''' | + | |
Press '''Enter '''to launch '''Interactive Ruby''' | Press '''Enter '''to launch '''Interactive Ruby''' | ||
|- | |- | ||
− | + | |Type | |
'''var1=10 '''<nowiki><<press </nowiki>'''Enter''' | '''var1=10 '''<nowiki><<press </nowiki>'''Enter''' | ||
− | + | |Now type '''var1''' equal to '''10 ''' | |
− | + | ||
− | + | ||
− | + | ||
− | '''var1''' equal to '''10 ''' | + | |
Press '''Enter''' | Press '''Enter''' | ||
|- | |- | ||
− | + | |Highlight '''var1''' | |
Highlight '''10''' | 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. | |
− | + | ||
− | '''integer '''or not | + | |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | + | |Type | |
'''var1.kind_of?Integer '''<nowiki><< press </nowiki>'''Enter''' | '''var1.kind_of?Integer '''<nowiki><< press </nowiki>'''Enter''' | ||
− | + | |So, type | |
'''var1 '''dot '''kind''' underscore '''of '''question mark '''Integer''' | '''var1 '''dot '''kind''' underscore '''of '''question mark '''Integer''' | ||
Line 161: | Line 125: | ||
|- | |- | ||
− | + | |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. | To do so, just assign a new value to it. | ||
Line 173: | Line 137: | ||
|- | |- | ||
− | + | |Type | |
'''var1=”hello”'''<nowiki><< press </nowiki>'''Enter''' | '''var1=”hello”'''<nowiki><< press </nowiki>'''Enter''' | ||
− | + | |Type | |
'''var1''' equal to within double quote '''hello''' | '''var1''' equal to within double quote '''hello''' | ||
− | press Enter | + | press '''Enter''' |
|- | |- | ||
− | + | |Type | |
'''<nowiki>var1.class<< </nowiki>'''press '''Enter''' | '''<nowiki>var1.class<< </nowiki>'''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 | Converting variable types | ||
− | |||
Highlight '''to_s''' | Highlight '''to_s''' | ||
− | |||
− | + | |'''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: | ||
|- | |- | ||
− | | | + | |Press '''Ctrl+L''' |
− | + | |Now let us try out these methods. | |
Go to the''' terminal''' | Go to the''' terminal''' | ||
− | + | Let's clear the '''terminal''' first. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | Press '''Ctrl + L''' to clear the '''irb '''console | ||
|- | |- | ||
− | + | |Type | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | '''<nowiki>y=20<<</nowiki>'''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 | We will now convert '''y''' to a floating point value using '''to '''underscore''' f''' method | ||
|- | |- | ||
− | + | |Type | |
'''<nowiki>y.to_f<< </nowiki>'''press''' Enter''' | '''<nowiki>y.to_f<< </nowiki>'''press''' Enter''' | ||
− | + | |Type '''y '''dot '''to '''underscore '''f''' and press '''Enter''' | |
− | + | ||
− | '''y '''dot '''to '''underscore '''f''' | + | |
− | + | ||
− | + | ||
|- | |- | ||
− | + | |Highlight''' 20.0''' | |
− | + | |We will get the value as '''float'''. | |
|- | |- | ||
− | + | |Type | |
'''<nowiki>y.to_s<<</nowiki>''' press''' Enter''' | '''<nowiki>y.to_s<<</nowiki>''' press''' Enter''' | ||
− | + | |Now type '''y '''dot '''to '''underscore '''s'''and press '''Enter''' | |
− | + | ||
− | '''y '''dot '''to '''underscore '''s | + | |
− | + | ||
− | + | ||
|- | |- | ||
− | + | |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<nowiki>(2)<<</nowiki>'''press''' Enter''' | |
− | + | |Now type '''opening bracket 2 closing bracket''' and press '''Enter''' | |
− | y.to_s | + | |
− | + | ||
− | + | ||
− | opening bracket 2 closing bracket | + | |
− | + | ||
− | 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. | By changing the number base to 8 or 16. | ||
|- | |- | ||
− | + | | | |
− | + | |Let us switch back to our slide | |
|- | |- | ||
− | + | |Slide 8 | |
− | + | ||
Variable Scope | 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 ''' | ||
− | |||
− | |||
|- | |- | ||
− | + | |Slide 9 | |
Naming Convention | Naming Convention | ||
− | + | |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 368: | Line 299: | ||
'''$ '''represents '''global''' variable | '''$ '''represents '''global''' variable | ||
− | Lower case letters | + | Lower case letters and underscore represents a '''local''' variable |
'''@''' represents an '''instance''' variable | '''@''' represents an '''instance''' variable | ||
Line 375: | Line 306: | ||
Upper case letters represents a '''constant''' | Upper case letters represents a '''constant''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
− | + | | | |
− | + | |We will learn in detail about this in another tutorial. | |
|- | |- | ||
− | + | | | |
− | + | |<nowiki><<Pause>></nowiki> | |
− | This brings us to the end of this Spoken Tutorial. | + | This brings us to the end of this Spoken Tutorial.let us summarise |
|- | |- | ||
− | + | |Slide 11 | |
− | + | ||
Summary | Summary | ||
− | + | |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''' | ||
− | |||
− | |||
|- | |- | ||
− | + | |Slide 13 | |
Assignment | 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 | ||
− | |||
− | |||
− | |||
|- | |- | ||
− | + | |Slide 14 | |
About the Spoken Tutorial Project | About the Spoken Tutorial Project | ||
− | + | |Watch the video available at the following link. | |
− | + | ||
It summarises the Spoken Tutorial project. | It summarises the Spoken Tutorial project. | ||
Line 425: | Line 346: | ||
|- | |- | ||
− | + | |Slide 15 | |
− | + | |The Spoken Tutorial Project Team : | |
− | + | ||
Conducts workshops using spoken tutorials | Conducts workshops using spoken tutorials | ||
Line 438: | Line 358: | ||
|- | |- | ||
− | + | |Slide 16 | |
Acknowledgement | 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. | 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
|
Slide 3
System Requirements |
Here we are using
|
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.
|
Slide 6
Variables |
* Variable names should be meaningful.
|
Slide 7
Dynamic Typing |
Now let us see what is dynamic typing
|
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
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:
|
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
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
|
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
|
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. |