Ruby/C2/Variables-in-Ruby/English
Title of script: Variables in Ruby
Author: Afrin pinjari
Keywords: to_s, to_i, video tutorial, local variable, global variable
|
|
---|---|
Slide 1 | |
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.
|
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
|
Open the terminal by pressing Ctrl,Alt and T keys simultaneously.
|
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
Press Enter |
Highlight string | We get the output as string |
Ruby has automatically changed the variable type from integer to string | |
Slide 7
Converting variable types
|
We will now learn how to convert a variable value to different type
Lets switch back to slides
The conversion depends on this number base. |
Press Ctrl+L | Now let us try out these methods.
Go to the terminal Lets clear the terminal first Press Ctrl,l to clear the irb console
|
Type
y=20<<press Enter |
Type
y equal to 20 Press Enter |
Highlight y and 20 | Here we have declared a variable 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 Press Enter |
Highlight 20.0 | We will get the value as float. |
Type
y.to_s<< press Enter |
Now Type
y dot to underscore s. 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 press Enter |
Highlight 10100 | We will get the output 10100 in 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
|
We will now learn what is variable scope.
|
Slide 9
Naming Convention |
Each variable type is declared by using a special character
At the beginning of the variable name
$ A global variable [a-z] or _ A local variable @ An instance variable @@ A class variable [A-Z] A constant
Lower case letters with 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. | |
Slide 11
|
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.
If you do not have good bandwidth, you can download and watch it. |
Slide 15 | The Spoken Tutorial Project Team :
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. |