1 00:00:00,420 --> 00:00:02,860 Hello and welcome to this lecture. 2 00:00:02,910 --> 00:00:08,850 In this lecture or Our be talking to you about variable scope in Ruby. 3 00:00:09,330 --> 00:00:19,050 What is a variable scope is scope basically describes the areas within a program where the variable 4 00:00:19,140 --> 00:00:22,220 is assessable from. 5 00:00:22,590 --> 00:00:28,110 There are several types of scopes available within Ruby. 6 00:00:28,320 --> 00:00:31,920 We've caught what is known as a local variable. 7 00:00:31,920 --> 00:00:38,370 You can usually tell the scope of a variable by their name. 8 00:00:38,580 --> 00:00:46,820 So with a local school the name is usually in law case or in lower case. 9 00:00:46,830 --> 00:00:53,850 But on score as well see you have to depend on the name have a variable that's called an amount on the 10 00:00:53,850 --> 00:00:55,830 score order. 11 00:00:56,040 --> 00:01:01,560 You can tell if you see a lower case you can tell that is the local variable or if it is just a variable 12 00:01:01,560 --> 00:01:04,140 chord amount or order. 13 00:01:04,140 --> 00:01:10,720 You can also tell as long as it's in lower case and it contains a character A to Z or an score. 14 00:01:10,890 --> 00:01:20,840 You can identify that as the local variable local variable available in a local scope. 15 00:01:20,970 --> 00:01:29,520 A little cool variable or lack of variables are local to the code construct in which they are declared. 16 00:01:29,520 --> 00:01:37,230 For example if you have a function and you declare a variable within that function that variable is 17 00:01:37,290 --> 00:01:41,100 only accessible from within that function. 18 00:01:41,160 --> 00:01:43,320 That will be the scope. 19 00:01:43,320 --> 00:01:50,670 We've also got a global scope of global variable and you can identify that here. 20 00:01:50,700 --> 00:01:55,700 The name of the variable has got a dollar sign in front of it. 21 00:01:55,830 --> 00:02:02,360 So global variables in Ruby are sensible from anywhere in the Ruby program. 22 00:02:02,700 --> 00:02:08,520 This is regardless of where they are hard to click so it doesn't matter where you declare the variable 23 00:02:08,520 --> 00:02:09,630 it is global. 24 00:02:09,780 --> 00:02:15,060 It is a sensible anywhere within the Ruby program. 25 00:02:15,150 --> 00:02:21,920 There are advantages disadvantage Surrey The Disadvantages of an arsehole is not encouraged. 26 00:02:21,920 --> 00:02:25,400 Not a lot of people in quarries using global variables. 27 00:02:25,410 --> 00:02:32,600 The reason being is that when you use global variables they are visible anywhere in the code or hip 28 00:02:32,630 --> 00:02:33,290 program. 29 00:02:33,380 --> 00:02:39,510 The code record for your program anywhere within that code you can see the global variables and they 30 00:02:39,510 --> 00:02:44,040 can also be changed from anywhere within the application. 31 00:02:44,040 --> 00:02:51,300 So this makes it very difficult to track any bug changes or any issues with the code makes it very difficult 32 00:02:51,390 --> 00:02:54,740 to track the global variable. 33 00:02:54,790 --> 00:02:57,450 If the scope is global. 34 00:02:58,010 --> 00:03:01,760 Next we've got the instance variables. 35 00:03:01,920 --> 00:03:08,950 You can identify that with the dollar sign then names usually have just at the last I'm sorry go. 36 00:03:08,970 --> 00:03:11,780 The at symbol in front of the name. 37 00:03:11,820 --> 00:03:19,410 So whereby you see the at symbol in front of a name it's an instance variable what is available within 38 00:03:19,920 --> 00:03:21,480 the instance. 39 00:03:21,660 --> 00:03:29,380 So basically an instance the values are local to specific instances of an object. 40 00:03:29,780 --> 00:03:37,110 Okay so a b will only be available within a specific instance of an object. 41 00:03:37,310 --> 00:03:42,550 Next we've also got what is known as a class scope. 42 00:03:42,750 --> 00:03:51,690 You can identify the variables in a class score because you've got double at symbol a class called classes 43 00:03:51,690 --> 00:04:03,180 in Ruby B's clean and first class objects so each class is an instance of a class of the ruby class. 44 00:04:03,180 --> 00:04:04,090 If you know what I mean. 45 00:04:04,290 --> 00:04:13,770 So with new classes created an object of the type class is in initialize had assigned a global constant 46 00:04:13,780 --> 00:04:19,560 So when you create the class you are beat you don't expect the name of the class to change. 47 00:04:19,560 --> 00:04:20,920 It becomes constant. 48 00:04:21,220 --> 00:04:24,430 Then body it will have other things within that class. 49 00:04:24,540 --> 00:04:26,330 But you can instantiate. 50 00:04:26,340 --> 00:04:34,020 You can create an instance of that class or the instances of that class or objects that belong to that 51 00:04:34,020 --> 00:04:34,430 class. 52 00:04:34,510 --> 00:04:38,500 All share the same variable scope. 53 00:04:38,640 --> 00:04:48,840 So a class variable is a variable that is shared among all the instances of a class with also got what 54 00:04:48,840 --> 00:04:51,270 is called a constant. 55 00:04:51,620 --> 00:04:54,160 Then you can identify a constant. 56 00:04:54,180 --> 00:04:56,380 By the way they are named. 57 00:04:56,610 --> 00:04:59,960 So with a constant you usually have it in capital capitals. 58 00:05:00,300 --> 00:05:03,000 Or the first letter is usually capital. 59 00:05:03,040 --> 00:05:11,830 You can identified because of capitals so Ruby constants are values which are which ones you've assigned 60 00:05:11,830 --> 00:05:15,240 the values are not meant to be changed. 61 00:05:15,420 --> 00:05:21,930 However Ruby does let you change them but it will warn you before you change them in all other programming 62 00:05:21,930 --> 00:05:22,680 languages. 63 00:05:22,680 --> 00:05:26,410 It doesn't you may not be able to do that once you've given it a value. 64 00:05:26,640 --> 00:05:31,510 But Ruby does let you change it but give me a warning before it does. 65 00:05:31,530 --> 00:05:40,470 Ruby also have what we called pseudo variables so this the variable variables which can not be assigned 66 00:05:40,470 --> 00:05:42,880 values you can't give them values. 67 00:05:42,990 --> 00:05:46,160 Example include Neil. 68 00:05:46,240 --> 00:05:53,100 Neil basically is is a sign to initialize a variable so when you create a variable you don't give it 69 00:05:53,100 --> 00:05:56,970 a value is referred to as nil. 70 00:05:57,040 --> 00:06:05,490 Also you've got self serve basically refers to the currently executing object. 71 00:06:05,580 --> 00:06:14,730 You can usually detect the scope of a variable by their names but you can also programmatically detect 72 00:06:14,880 --> 00:06:17,460 what scope a variable in. 73 00:06:17,910 --> 00:06:22,030 So what do we do that there is a method you can use. 74 00:06:22,050 --> 00:06:28,860 So let's say I created a variable and then give you the value of 7. 75 00:06:29,460 --> 00:06:32,510 Okay so we know that variable is 7. 76 00:06:32,520 --> 00:06:36,530 So if I want to find out what scope that variable it's in. 77 00:06:37,400 --> 00:06:41,780 Already know it's a local variable because it's in lower case. 78 00:06:41,820 --> 00:06:46,460 Well I can also find that out by type name the method defined. 79 00:06:48,440 --> 00:06:50,530 Followed by a question mark. 80 00:06:51,400 --> 00:06:54,980 Space and then a variable name and press centre. 81 00:06:55,240 --> 00:06:59,420 You can see it told me that is a local variable. 82 00:06:59,800 --> 00:07:10,530 So if I did the same thing I created say I have a dollar sign X and said that two equals to seven. 83 00:07:10,970 --> 00:07:15,610 If I come here and do find. 84 00:07:17,310 --> 00:07:18,630 Question mark. 85 00:07:19,760 --> 00:07:26,550 And then Darla X here to me this gorp is global. 86 00:07:26,580 --> 00:07:28,380 So you know you get the idea. 87 00:07:28,380 --> 00:07:34,470 So you can do the same once you know the symbol of how to identify. 88 00:07:34,470 --> 00:07:39,370 You can usually identify by the by the symbols in front of variable names. 89 00:07:39,690 --> 00:07:46,500 So you know that you can use that to identify or you can do programmatically I have done here. 90 00:07:46,710 --> 00:07:49,070 So that's basically it. 91 00:07:49,080 --> 00:07:52,920 How about variable scopes in Ruby. 92 00:07:53,010 --> 00:07:54,110 Thanks for watching. 93 00:07:54,150 --> 00:07:56,700 I hope it has made sense if it hasn't. 94 00:07:56,700 --> 00:07:58,220 Please let me know. 95 00:07:58,500 --> 00:08:01,130 I'll be more than happy to explain further. 96 00:08:01,260 --> 00:08:02,070 Thanks for watching. 97 00:08:02,070 --> 00:08:03,030 Thanks for your time. 98 00:08:03,070 --> 00:08:03,370 Bye.