Ruby/C2/Variables-in-Ruby/English-timed
From Script | Spoken-Tutorial
Time' | Narration |
00.03 | Welcome to the Spoken Tutorial onVariables in Ruby |
00.07 | In this tutorial we will learn |
00.09 | What is a variable? |
00.11 | Dynamic typing in Ruby |
00.13 | Declaring a variable |
00.15 | Converting variable types |
00.18 | What is variable's scope? |
00.20 | Types of variables |
00.23 | Here we are using Ubuntu Linux version 12.04 Ruby 1.9.3
|
00.32 | To follow this tutorial you must have knowledge of using Terminal in Linux.
|
00.38 | You must also be familiar with irb
|
00.41 | If not, for relevant tutorials, please visit our website
|
00.47 | Now I will explain what a variable is.
|
00.51 | Variable is used to store a value. |
00.55 | Variable is a reference that can be assigned. |
00.59 | Please note that Ruby variables are case sensitive.
|
01.04 | Variable names should be meaningful. |
01.07 | Variable name may only contain lowercase letters, numbers and underscores. Ex : first_name
|
01.20 | Now let us see what is dynamic typing |
01.24 | Ruby is a dynamic typed language. |
01.28 | It means that you don't need to declare datatype while creating a variable. |
01.34 | Ruby interpreter determines the data type at the time of assignment.
|
01.39 | Now let us see how to declare a variable in Ruby. |
01.45 | Open the terminal by pressing Ctrl,Alt and T keys simultaneously.
|
01.51 | A terminal window appears on your screen. |
01.55 | Type' 'irb |
01.57 | Press Enter to launch Interactive Ruby |
02.02 | Now type var1 equal to 10 andPress Enter |
02.09 | Here we have declared a variable var1 and assigned a value 10 to it. |
02.15 | Let's check whether the datatype allotted by the interpreter is integer or not .
|
02.21 | So, type var1 dot kind underscore of question mark Integerand press Enter |
02.37 | We will get the output as true. |
02.40 | In Ruby you can dynamically change the variable type. |
02.44 | To do so, just assign a new value to it. |
02.48 | Let's do this by assigning a string value to variable var1. |
02.53 | Type var1 equal to within double quote helloand press Enter |
03.03 | Let's verify the variable type assigned |
03.06 | Type var1 dot class
|
03.12 | Class method tells us what class of variable it is. Now Press Enter |
03.20 | We get the output as string |
03.20 | Ruby has automatically changed the variable type from integer to string |
03.30 | We will now learn how to convert a variable value to different type |
03.35 | Lets switch back to slides
|
03.39 | Ruby variable classes have methods to convert their value to a different type |
03.45 | to_i method is used to convert a variable to integer |
03.51 | to_f method is used to convert a variable to floating point value |
03.57 | to_s method is used to convert a variable to string
|
04.03 | The to _ s() method takes number base as an argument. |
04.08 | The conversion depends on this number base. |
04.12 | Now let us try out these methods. |
04.15 | Go to the terminal Lets clear the terminal first |
04.21 | Press Ctrl,l to clear the irb console
|
04.25 | Now Type y equal to 20 and Press Enter |
04.32 | Here we have declared a variable called y and assigned a value 20 to it. |
04.40 | We will now convert y to a floating point value using to underscore f method |
04.47 | Type y dot to underscore f and Press Enter |
04.55 | We will get the value as float. |
04.58 | Now Type y dot to underscore s. and Press Enter |
05.06 | We will get output 20 within double quotes |
05.11 | To convert variable y in binary form give number base as 2 in to_s method |
05.18 | Press up arrow key to get the previous command |
05.22 | Now type opening bracket 2 closing bracket and press Enter |
05.30 | We will get the output 10100 in the binary form |
05.33 | Similarly you can convert variable y to octal or hexadecimal form |
05.39 | By changing the number base to 8 or 16. |
05.44 | Let us switch back to our slide |
05.48 | We will now learn what is a variable scope.
|
05.52 | Scope defines where in a program a variable is accessible.
|
05.56 | Ruby has four types of variable scope:
|
06.06 | Each variable type is declared by using a special character |
06.11 | At the beginning of the variable name
|
06.14 | $ represents global variable |
06.18 | Lower case letters and underscore represents a local variable |
06.25 | @ represents an instance variable |
06.29 | Two @@ represents a class variable |
06.33 | Upper case letters represents a constant
|
06.37 | We will learn in detail about this in another tutorial. |
06.42 | This brings us to the end of this Spoken Tutorial. Let us summarises |
06.49 | In this tutorial we have learnt |
06.51 | To declare a variable eg var1=10 |
06.56 | Changing variable type using to_f, to_s methods |
07.04 | Different Variable scope
|
07.07 | As an assignment |
07.08 | Declare a variable and convert it to octal and hexadecimal form
|
07.14 | Watch the video available at the following link.
|
07.17 | It summarises the Spoken Tutorial project. |
07.20 | If you do not have good bandwidth, you can download and watch it. |
07.25 | The Spoken Tutorial Project Team :
|
07.27 | Conducts workshops using spoken tutorials |
07.30 | Gives certificates to those who pass an online test |
07.34 | For more details, please write to contact at spoken hyphen tutorial dot org |
07.41 | Spoken Tutorial Project is a part of the Talk to a Teacher project. |
07.46 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
07.52 | More information on this Mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro. |
07.57 | This is Afrin Pinjari from IIT Bombay, signing off.
Thank you for watching. |