Difference between revisions of "Ruby/C2/Variables-in-Ruby/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
|||
(9 intermediate revisions by 2 users not shown) | |||
Line 5: | Line 5: | ||
|- | |- | ||
| 00:02 | | 00:02 | ||
− | | | + | | Welcome to the '''Spoken Tutorial''' on '''Variables in Ruby'''. |
|- | |- | ||
| 00:06 | | 00:06 | ||
− | | In this tutorial we will learn | + | | In this tutorial, we will learn: |
|- | |- | ||
| 00:09 | | 00:09 | ||
− | | What is a variable? | + | | What is a variable? Dynamic typing in '''Ruby''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 00:13 | | 00:13 | ||
− | | | + | | Declaring a variable |
|- | |- | ||
| 00:15 | | 00:15 | ||
− | | | + | | Converting variable types |
|- | |- | ||
| 00:18 | | 00:18 | ||
− | | | + | | What is variable's scope? |
|- | |- | ||
| 00:20 | | 00:20 | ||
− | | | + | | Types of variables. |
|- | |- | ||
| 00:23 | | 00:23 | ||
− | | Here we are using | + | | Here, we are using: '''Ubuntu Linux''' version '''12.04''', '''Ruby 1.9.3''' |
|- | |- | ||
| 00:32 | | 00:32 | ||
− | | To follow this tutorial you must have the knowledge of using''' Terminal''' in '''Linux.''' | + | | To follow this tutorial, you must have the knowledge of using''' Terminal''' in '''Linux.''' |
− | + | ||
|- | |- | ||
| 00:38 | | 00:38 | ||
− | | You must also be familiar with '''irb''' | + | | You must also be familiar with '''"irb"'''. |
− | + | ||
|- | |- | ||
| 00:41 | | 00:41 | ||
− | | If not, for relevant tutorials, please visit our website | + | | If not, for relevant tutorials, please visit our website. |
− | + | ||
|- | |- | ||
| 00:47 | | 00:47 | ||
− | | Now I will explain what a variable is. | + | | Now, I will explain you what a '''variable''' is. |
|- | |- | ||
Line 68: | Line 61: | ||
|- | |- | ||
| 00:58 | | 00:58 | ||
− | | Please note that '''Ruby ''' | + | | Please note that '''Ruby variables''' are '''case sensitive.''' |
|- | |- | ||
Line 80: | Line 73: | ||
|- | |- | ||
| 01:20 | | 01:20 | ||
− | | Now let us see what is dynamic typing | + | | Now, let us see what is '''dynamic typing'''. |
|- | |- | ||
Line 87: | Line 80: | ||
|- | |- | ||
| 01:27 | | 01:27 | ||
− | | It means that you don't need to declare | + | | It means that you don't need to declare datatype while creating a variable. |
|- | |- | ||
| 01:34 | | 01:34 | ||
− | | '''Ruby interpreter '''determines the | + | | '''Ruby interpreter '''determines the datatype at the time of assignment. |
− | + | ||
− | + | ||
|- | |- | ||
| 01:39 | | 01:39 | ||
− | | Now let us see how to declare a variable in | + | | Now, let us see how to declare a variable in Ruby. |
|- | |- | ||
| 01:45 | | 01:45 | ||
− | | Open the terminal by pressing '''Ctrl, Alt''' and''' T '''keys simultaneously. | + | | Open the '''terminal''' by pressing '''Ctrl, Alt''' and''' T '''keys simultaneously. |
− | + | ||
|- | |- | ||
| 01:51 | | 01:51 | ||
− | | A terminal window appears on your screen. | + | | A '''terminal window''' appears on your screen. |
|- | |- | ||
| 01:55 | | 01:55 | ||
− | | Now, type | + | | Now, type "irb" and |
|- | |- | ||
| 01:57 | | 01:57 | ||
− | | | + | | press '''Enter '''to launch '''Interactive Ruby'''. |
|- | |- | ||
| 02:02 | | 02:02 | ||
− | | Now type '''var1''' equal to 10 and | + | | Now, type: '''var1''' equal to 10 and press '''Enter'''. |
|- | |- | ||
| 02:09 | | 02:09 | ||
− | | 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. |
|- | |- | ||
| 02:15 | | 02:15 | ||
| Let's check whether the datatype allotted by the interpreter is integer or not. | | Let's check whether the datatype allotted by the interpreter is integer or not. | ||
− | |||
|- | |- | ||
| 02:21 | | 02:21 | ||
− | | So, type '''var1''' dot '''kind'''_(underscore)'''of '''(?)question mark '''Integer''' and press '''Enter''' | + | | So, type: '''var1''' dot '''kind'''_(underscore)'''of '''(?)question mark '''Integer''' and press '''Enter'''. |
|- | |- | ||
| 02:37 | | 02:37 | ||
− | | We | + | | We get the output as "true". |
|- | |- | ||
| 02:39 | | 02:39 | ||
− | | In''' Ruby''' you can dynamically change the variable type. | + | | In''' Ruby''', you can dynamically change the variable type. |
|- | |- | ||
Line 151: | Line 140: | ||
|- | |- | ||
| 02:53 | | 02:53 | ||
− | | | + | | Type: '''var1''' equal to within double quotes '''hello''' and press '''Enter'''. |
|- | |- | ||
| 03:02 | | 03:02 | ||
− | | Let's verify the variable type assigned | + | | Let's verify the variable type assigned. |
|- | |- | ||
| 03:06 | | 03:06 | ||
− | | Type '''var1''' dot '''class ''' | + | | Type: '''var1''' dot '''class '''. |
− | + | ||
|- | |- | ||
| 03:12 | | 03:12 | ||
− | | Class method tells us what class of variable it is. Now | + | | '''Class''' method tells us what class of variable it is. Now press ''' Enter'''. |
|- | |- | ||
| 03:20 | | 03:20 | ||
− | | We get the output as | + | | We get the output as "String". |
|- | |- | ||
| 03:23 | | 03:23 | ||
− | | | + | | Ruby has automatically changed the variable type from '''integer''' to '''string'''. |
|- | |- | ||
| 03:29 | | 03:29 | ||
− | | We will now learn how to convert a variable value to different type | + | | We will now learn how to convert a variable value to different type. |
|- | |- | ||
| 03:35 | | 03:35 | ||
− | | | + | | Let's switch back to slide. |
− | + | ||
|- | |- | ||
| 03:38 | | 03:38 | ||
− | | | + | | Ruby variable classes have methods to convert their value to a different type. |
|- | |- | ||
| 03:45 | | 03:45 | ||
− | | '''to_i '''method is used to convert a variable to '''integer''' | + | | ''''to_i'''' method is used to convert a variable to '''integer'''. |
+ | |||
|- | |- | ||
| 03:51 | | 03:51 | ||
− | | '''to_f '''method is used to convert a variable to '''floating point value''' | + | | ''''to_f'''' method is used to convert a variable to '''floating point value'''. |
+ | |||
|- | |- | ||
| 03:57 | | 03:57 | ||
− | | '''to_s''' method is used to convert a variable to''' string''' | + | | ''''to_s'''' method is used to convert a variable to''' string'''. |
− | + | ||
|- | |- | ||
| 04:03 | | 04:03 | ||
− | | '''to _s''' method takes number base as an argument. | + | | ''''to _s'''' method takes '''number base''' as an '''argument'''. |
|- | |- | ||
Line 208: | Line 196: | ||
|- | |- | ||
| 04:12 | | 04:12 | ||
− | | Now let us try out these methods. | + | | Now, let us try out these methods. |
|- | |- | ||
| 04:15 | | 04:15 | ||
− | | Go to the''' terminal''' | + | | Go to the''' terminal'''. Let's clear the terminal first. |
|- | |- | ||
| 04:21 | | 04:21 | ||
− | | Press Ctrl L to clear the '''irb '''console | + | | Press '''Ctrl, L''' to clear the '''irb '''console. |
− | + | ||
|- | |- | ||
| 04:25 | | 04:25 | ||
− | | Now | + | | Now, type: '''y '''equal to '''20''' and press '''Enter'''. |
|- | |- | ||
| 04:32 | | 04:32 | ||
− | | Here we have declared a variable called | + | | Here, we have declared a variable called 'y' and assigned a value '''20''' to it. |
|- | |- | ||
| 04:39 | | 04:39 | ||
− | | We will now convert ''' | + | | We will now convert 'y' to a '''floating point value''' using '''to underscore f''' method. |
|- | |- | ||
| 04:47 | | 04:47 | ||
− | | Type '''y '''dot '''to | + | | Type: '''y '''dot '''to underscore f''' and press '''Enter'''. |
|- | |- | ||
| 04:55 | | 04:55 | ||
− | | We | + | | We get the value as '''float'''. |
|- | |- | ||
| 04:57 | | 04:57 | ||
− | | Now | + | | Now, type: '''y '''dot '''to underscore s''' and press '''Enter'''. |
|- | |- | ||
| 05:06 | | 05:06 | ||
− | | We | + | | We get the output as 20, within double quotes. |
|- | |- | ||
| 05:10 | | 05:10 | ||
− | | To convert variable | + | | To convert variable 'y' in binary form, give '''number base''' as '''2''' in the '''to_s''' method. |
|- | |- | ||
| 05:18 | | 05:18 | ||
− | | Press up arrow key to get the previous command | + | | Press the up-arrow key to get the previous command. |
|- | |- | ||
| 05:22 | | 05:22 | ||
− | | Type opening bracket 2 closing bracket and press '''Enter''' | + | | Type: opening bracket 2 closing bracket and press '''Enter'''. |
|- | |- | ||
| 05:29 | | 05:29 | ||
− | | We | + | | We get the output in the '''binary''' form. |
|- | |- | ||
| 05:33 | | 05:33 | ||
− | | | + | | Similarly, you can convert variable 'y' to '''octal''' or '''hexadecimal''' form |
|- | |- | ||
| 05:39 | | 05:39 | ||
− | | | + | | by changing the number base to '''8''' or '''16'''. |
|- | |- | ||
| 05:44 | | 05:44 | ||
− | | Let us switch back to our slide | + | | Let us switch back to our '''slide'''. |
|- | |- | ||
| 05:47 | | 05:47 | ||
| We will now learn what is a '''variable scope'''. | | We will now learn what is a '''variable scope'''. | ||
− | |||
|- | |- | ||
| 05:51 | | 05:51 | ||
− | | '''Scope''' defines where in a program a variable is accessible. | + | | '''Scope''' defines- where in a program, a variable is accessible. |
− | + | ||
|- | |- | ||
| 05:56 | | 05:56 | ||
− | | '''Ruby''' has four types of variable | + | | '''Ruby''' has four types of variable scopes: |
|- | |- | ||
| 06:00 | | 06:00 | ||
− | | Local | + | | Local, Global |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 06:02 | | 06:02 | ||
− | | Instance and | + | |Instance and |
|- | |- | ||
| 06:04 | | 06:04 | ||
− | | Class | + | | Class. |
|- | |- | ||
| 06:06 | | 06:06 | ||
− | | Each variable type is declared by using a special character at the beginning of the variable name | + | | Each variable type is declared by using a special character at the beginning of the variable name. |
− | + | ||
− | + | ||
|- | |- | ||
− | | | + | | 06:14 |
− | | | + | | '$' represents '''global''' variable. |
|- | |- | ||
| 06:18 | | 06:18 | ||
− | | Lower case letters and | + | | Lower case letters and underscore represent a '''local''' variable. |
|- | |- | ||
| 06:25 | | 06:25 | ||
− | | | + | | '@' represents an '''instance''' variable. |
|- | |- | ||
| 06:29 | | 06:29 | ||
− | | Two | + | | Two '@@' symbols represent a '''class''' variable. |
|- | |- | ||
| 06:33 | | 06:33 | ||
− | | Upper case letters | + | | Upper case letters represent a '''constant'''. |
− | + | ||
|- | |- | ||
| 06:37 | | 06:37 | ||
− | | We will learn in detail about this in another tutorial. | + | | We will learn in detail, about this, in another tutorial. |
|- | |- | ||
| 06:42 | | 06:42 | ||
− | | This brings us to the end of this Spoken Tutorial. Let us | + | | This brings us to the end of this Spoken Tutorial. Let us summarize. |
|- | |- | ||
| 06:48 | | 06:48 | ||
− | | In this tutorial we have learnt | + | | In this tutorial, we have learnt: |
|- | |- | ||
| 06:51 | | 06:51 | ||
− | | | + | |To declare a variable, e.g. var1=10 |
+ | |||
|- | |- | ||
| 06:56 | | 06:56 | ||
− | | | + | | Changing a variable type using 'to_f' and 'to_s' methods |
+ | |||
|- | |- | ||
| 07:04 | | 07:04 | ||
− | | | + | | Different variable '''scope'''s. |
− | + | ||
− | + | ||
|- | |- | ||
| 07:06 | | 07:06 | ||
− | | As an assignment | + | | As an assignment, |
|- | |- | ||
| 07:08 | | 07:08 | ||
− | | | + | | declare a variable and convert it to '''octal''' and '''hexadecimal''' form. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 07:14 | | 07:14 | ||
| Watch the video available at the following link. | | Watch the video available at the following link. | ||
− | |||
|- | |- | ||
| 07:17 | | 07:17 | ||
− | | It | + | | It summarizes the Spoken Tutorial project. |
|- | |- | ||
Line 382: | Line 356: | ||
|- | |- | ||
| 07:24 | | 07:24 | ||
− | | The Spoken Tutorial | + | | The Spoken Tutorial project team : |
− | + | ||
|- | |- | ||
| 07:27 | | 07:27 | ||
− | | Conducts workshops using spoken tutorials | + | | Conducts workshops using spoken tutorials. |
|- | |- | ||
| 07:30 | | 07:30 | ||
− | | Gives certificates to those who pass an online test | + | | Gives certificates to those who pass an online test. |
|- | |- | ||
| 07:34 | | 07:34 | ||
− | | For more details, please write to contact | + | | For more details, please write to: contact @ spoken hyphen tutorial dot org. |
|- | |- | ||
| 07:41 | | 07:41 | ||
− | | | + | | '''Spoken Tutorial''' project is a part of the '''Talk to a Teacher''' project. |
|- | |- | ||
Line 407: | Line 380: | ||
|- | |- | ||
| 07:51 | | 07:51 | ||
− | | More information on this | + | | More information on this mission is available at the below link. |
|- | |- | ||
| 07:57 | | 07:57 | ||
− | | This is Afrin Pinjari from IIT Bombay, signing off. | + | | This is Afrin Pinjari from '''IIT Bombay''', signing off. Thank you for watching. |
− | + | ||
− | Thank you for watching. | + | |
|} | |} |
Latest revision as of 12:00, 10 March 2017
Time | Narration |
00:02 | Welcome to the Spoken Tutorial on Variables in Ruby. |
00:06 | In this tutorial, we will learn: |
00:09 | What is a variable? 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 the 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 you what a variable is. |
00:50 | Variable is used to store a value. |
00:54 | Variable is a reference that can be assigned. |
00:58 | 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, underscores. Ex : first_name |
01:20 | Now, let us see what is dynamic typing. |
01:23 | Ruby is a dynamic typed language. |
01:27 | It means that you don't need to declare datatype while creating a variable. |
01:34 | Ruby interpreter determines the datatype 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 | Now, type "irb" and |
01:57 | press Enter to launch Interactive Ruby. |
02:02 | Now, type: var1 equal to 10 and press 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 Integer and press Enter. |
02:37 | We get the output as "true". |
02:39 | In Ruby, you can dynamically change the variable type. |
02:44 | To do so, just assign a new value to it. |
02:47 | Let's do this by assigning a string value to variable var1. |
02:53 | Type: var1 equal to within double quotes hello and press Enter. |
03:02 | 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:23 | Ruby has automatically changed the variable type from integer to string. |
03:29 | We will now learn how to convert a variable value to different type. |
03:35 | Let's switch back to slide. |
03:38 | 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 | '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. Let's 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:39 | 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 get the value as float. |
04:57 | Now, type: y dot to underscore s and press Enter. |
05:06 | We get the output as 20, within double quotes. |
05:10 | To convert variable 'y' in binary form, give number base as 2 in the to_s method. |
05:18 | Press the up-arrow key to get the previous command. |
05:22 | Type: opening bracket 2 closing bracket and press Enter. |
05:29 | We get the output 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:47 | We will now learn what is a variable scope. |
05:51 | Scope defines- where in a program, a variable is accessible. |
05:56 | Ruby has four types of variable scopes: |
06:00 | Local, Global |
06:02 | Instance and |
06:04 | Class. |
06:06 | Each variable type is declared by using a special character at the beginning of the variable name. |
06:14 | '$' represents global variable. |
06:18 | Lower case letters and underscore represent a local variable. |
06:25 | '@' represents an instance variable. |
06:29 | Two '@@' symbols represent a class variable. |
06:33 | Upper case letters represent 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 summarize. |
06:48 | In this tutorial, we have learnt: |
06:51 | To declare a variable, e.g. var1=10 |
06:56 | Changing a variable type using 'to_f' and 'to_s' methods |
07:04 | Different variable scopes. |
07:06 | 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 summarizes the Spoken Tutorial project. |
07:20 | If you do not have good bandwidth, you can download and watch it. |
07:24 | 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 @ spoken hyphen tutorial dot org. |
07:41 | Spoken Tutorial project is a part of the Talk to a Teacher project. |
07:45 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
07:51 | More information on this mission is available at the below link. |
07:57 | This is Afrin Pinjari from IIT Bombay, signing off. Thank you for watching. |