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;…') |
|||
Line 197: | Line 197: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight''' .class''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight''' .class''' | ||
− | | 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 | + | | 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 |
− | Press''' Enter''' | + | Now Press''' Enter''' |
|- | |- | ||
Line 249: | Line 249: | ||
'''<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;"| Type | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now Type |
'''y '''equal to '''20''' | '''y '''equal to '''20''' | ||
− | Press''' '''Enter | + | 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''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| 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''' y''' and assigned a value '''20''' to it. | + | | 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. |
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 | ||
Line 267: | Line 267: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type | ||
− | '''y '''dot '''to '''underscore '''f''' | + | '''y '''dot '''to '''underscore '''f''' and |
Press '''Enter''' | Press '''Enter''' | ||
Line 281: | Line 281: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now Type | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now Type | ||
− | '''y '''dot '''to '''underscore '''s.''' | + | '''y '''dot '''to '''underscore '''s.'''and |
Press '''Enter''' | Press '''Enter''' | ||
Line 306: | Line 306: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now type | | 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 | + | opening bracket 2 closing bracket and |
press '''Enter''' | press '''Enter''' | ||
Line 312: | Line 312: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''10100''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| 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 binary form | + | | 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 |
|- | |- | ||
Line 368: | Line 368: | ||
'''$ '''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 387: | Line 387: | ||
| 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> | | 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> | ||
− | This brings us to the end of this Spoken Tutorial. | + | This brings us to the end of this Spoken Tutorial.let us summarise |
|- | |- |
Revision as of 12:05, 16 April 2013
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 and
Now 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 |
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 s.and 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
|
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 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
|
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. |