0:00:04.720,0:00:09.280 this problem has given us 0:00:06.399,0:00:10.240 two pieces of code and we're going to 0:00:09.280,0:00:12.799 examine them 0:00:10.240,0:00:14.880 each and determine what the program will 0:00:12.799,0:00:17.600 output 0:00:14.880,0:00:18.400 so looking at the first piece of code on 0:00:17.600,0:00:21.199 line one 0:00:18.400,0:00:22.080 a local variable x is initialized and it 0:00:21.199,0:00:25.439 references 0:00:22.080,0:00:26.400 the integer zero we then invoke the 0:00:25.439,0:00:29.039 times method 0:00:26.400,0:00:32.079 on the integer three and we've passed 0:00:29.039,0:00:34.160 the block to this times method 0:00:32.079,0:00:37.120 so ruby's going to iterate through this 0:00:34.160,0:00:38.960 block three times 0:00:37.120,0:00:40.719 the code on line three may look a little 0:00:38.960,0:00:42.160 bit confusing if you haven't seen it 0:00:40.719,0:00:45.520 before 0:00:42.160,0:00:49.440 this is shorthand 0:00:45.520,0:00:51.840 for x equals x plus one 0:00:49.440,0:00:53.280 so this operation here on line three is 0:00:51.840,0:00:57.039 reassignment 0:00:53.280,0:01:00.719 we are reassigning x to the value of x 0:00:57.039,0:01:02.160 plus 1. so on our first iteration 0:01:00.719,0:01:05.280 through this block 0:01:02.160,0:01:08.960 x will be reassigned to the integer 1 0:01:05.280,0:01:11.439 then 2 and then 3. then ruby stops 0:01:08.960,0:01:13.760 iterating through this block 0:01:11.439,0:01:14.640 and our program execution reaches line 0:01:13.760,0:01:17.360 five 0:01:14.640,0:01:17.840 on line five we invoke the put us method 0:01:17.360,0:01:21.520 to 0:01:17.840,0:01:24.080 output x because we know that we have 0:01:21.520,0:01:27.200 iterated through this block three times 0:01:24.080,0:01:29.280 incrementing the integer referenced by x 0:01:27.200,0:01:32.079 we expect that x now references the 0:01:29.280,0:01:32.079 integer three 0:01:32.240,0:01:37.040 and that's what we expect to see when we 0:01:34.240,0:01:41.840 output x on line five 0:01:37.040,0:01:41.840 so let's run our code and see that 0:01:43.600,0:01:47.520 and we see three as expected 0:01:50.159,0:01:55.840 so let's take a look at the second piece 0:01:52.479,0:01:55.840 of code we were given 0:01:56.320,0:02:00.560 this code is a little bit different we 0:01:58.880,0:02:05.200 have two variables 0:02:00.560,0:02:08.399 y and x y is initialized on line one 0:02:05.200,0:02:10.879 and it references zero again we invoke 0:02:08.399,0:02:12.480 the times method on the integer 3 and 0:02:10.879,0:02:15.040 pass it a block 0:02:12.480,0:02:16.160 within the block we are going to 0:02:15.040,0:02:19.599 reassign y 0:02:16.160,0:02:21.280 to y plus 1 each time we iterate through 0:02:19.599,0:02:25.360 that's the behavior we saw with the 0:02:21.280,0:02:27.520 variable x in our previous piece of code 0:02:25.360,0:02:29.280 then on line 4 you can see that we 0:02:27.520,0:02:32.080 initialize a local variable 0:02:29.280,0:02:34.080 x and we point it at the same value 0:02:32.080,0:02:35.920 referenced by y 0:02:34.080,0:02:38.879 so as we iterate through this block 0:02:35.920,0:02:42.800 three times y will be reassigned to one 0:02:38.879,0:02:43.200 two and then three when the code on line 0:02:42.800,0:02:45.680 four 0:02:43.200,0:02:48.080 runs x will also be assigned to those 0:02:45.680,0:02:50.720 integers 0:02:48.080,0:02:51.599 so when program execution reaches line 0:02:50.720,0:02:55.440 six and we 0:02:51.599,0:02:57.680 output x you may expect to see three 0:02:55.440,0:02:59.040 however there is a problem here because 0:02:57.680,0:03:02.560 unlike y 0:02:59.040,0:03:06.000 x was initialized within the block 0:03:02.560,0:03:08.159 blocks create an inner scope variables 0:03:06.000,0:03:11.360 initialized within an inner scope 0:03:08.159,0:03:13.680 are not available in an outer scope 0:03:11.360,0:03:19.280 on line six we expect that ruby will 0:03:13.680,0:03:22.159 raise an error 0:03:19.280,0:03:24.000 we run our code and we see this error we 0:03:22.159,0:03:24.400 can see that the problem occurred on 0:03:24.000,0:03:26.959 line 0:03:24.400,0:03:27.840 6 and the error message tells us we have 0:03:26.959,0:03:33.840 an undefined 0:03:27.840,0:03:33.840 local variable or method x 0:03:33.920,0:03:38.959 because x was initialized in that inner 0:03:36.319,0:03:40.319 scope ruby cannot see what x is on line 0:03:38.959,0:03:43.680 six 0:03:40.319,0:03:47.840 however we can output y and if we run 0:03:43.680,0:03:47.840 our code we'll see that 0:03:48.879,0:03:52.879 the difference is that y was initialized 0:03:51.760,0:03:56.159 on line 1 0:03:52.879,0:03:58.720 in the outer scope interscopes 0:03:56.159,0:03:59.760 can access variables initialized in the 0:03:58.720,0:04:01.840 outer scope 0:03:59.760,0:04:03.280 however the outer scope cannot access 0:04:01.840,0:04:07.040 variables initialized 0:04:03.280,0:04:07.599 in the inner scope in order to output x 0:04:07.040,0:04:10.640 on line 0:04:07.599,0:04:14.560 6 we would need to initialize x in the 0:04:10.640,0:04:17.759 outer scope 0:04:14.560,0:04:21.199 so if we initialize x on line two 0:04:17.759,0:04:22.160 and run our code again our code now runs 0:04:21.199,0:04:26.560 without error 0:04:22.160,0:04:26.560 and is able to output x from outside the 0:04:26.840,0:04:29.840 block 0:04:30.479,0:04:34.479 looking back at our first snippet of 0:04:32.160,0:04:35.680 code which i've pasted on lines 8 0:04:34.479,0:04:38.320 through 12 0:04:35.680,0:04:39.360 you can see the same behavior there x 0:04:38.320,0:04:42.639 was initialized 0:04:39.360,0:04:45.120 on line 8 in the outer scope 0:04:42.639,0:04:48.240 it was accessible inside the inner scope 0:04:45.120,0:04:50.800 of the block on line 10 0:04:48.240,0:04:57.840 and was successfully output on line 12 0:04:50.800,0:04:57.840 in the outer scope 0:04:58.880,0:05:00.960 you