1 00:00:00,130 --> 00:00:03,650 Your goal was to build a simple Ruby program that calculates the area and 2 00:00:03,650 --> 00:00:05,660 perimeter of a rectangle. 3 00:00:05,660 --> 00:00:07,400 Here's my solution. 4 00:00:07,400 --> 00:00:09,140 It's okay if yours is slightly different. 5 00:00:09,140 --> 00:00:11,300 But if you see something interesting in my code, 6 00:00:11,300 --> 00:00:15,685 you should consider borrowing it to improve your own own program. 7 00:00:15,685 --> 00:00:17,815 So up here I have defined an area method. 8 00:00:17,815 --> 00:00:22,165 And it takes two parameters, a length and a width. 9 00:00:22,165 --> 00:00:26,665 And that's all that we need to calculate an area for a rectangle. 10 00:00:26,665 --> 00:00:31,040 We just multiply the length by the width, store that in a variable, and 11 00:00:31,040 --> 00:00:32,245 then return that variable. 12 00:00:35,130 --> 00:00:38,030 Next step, we were supposed to define a perimeter method. 13 00:00:38,030 --> 00:00:42,670 Which, if you visualize a rectangle, there are two sides where the length is 14 00:00:42,670 --> 00:00:45,260 the same and two sides where the width is the same. 15 00:00:45,260 --> 00:00:48,030 So you just add the length and the width together and 16 00:00:48,030 --> 00:00:50,700 multiply the whole thing by two. 17 00:00:50,700 --> 00:00:55,980 Making sure to complete the addition operation before doing the multiplication. 18 00:00:55,980 --> 00:00:59,960 You can use parentheses to ensure that the correct order of operations is 19 00:00:59,960 --> 00:01:00,480 followed there. 20 00:01:02,380 --> 00:01:07,680 So we just accept length and width parameters, add those together, 21 00:01:07,680 --> 00:01:09,550 multiply the whole thing by 2, 22 00:01:09,550 --> 00:01:13,110 assign that to a variable, and return the value of that variable. 23 00:01:14,720 --> 00:01:19,002 Then down here we were supposed to call the methods that we've created. 24 00:01:19,002 --> 00:01:22,892 So we make a call to the area method, 25 00:01:22,892 --> 00:01:27,704 we pass it a length and a width as arguments. 26 00:01:27,704 --> 00:01:31,132 And here we make a call to perimeter, again passing a length and 27 00:01:31,132 --> 00:01:33,020 a width as arguments. 28 00:01:33,020 --> 00:01:37,930 We take the return values of each of those methods and 29 00:01:37,930 --> 00:01:42,820 pass those in turn to the puts method which just prints those values out. 30 00:01:42,820 --> 00:01:47,504 So we make a call here to area, with a length of 2 and a width of 4. 31 00:01:47,504 --> 00:01:52,030 It multiplies those two together and we get a resulting area of 8. 32 00:01:52,030 --> 00:01:56,010 Here, we make a call to perimeter, with a length of 2 and a width of 4. 33 00:01:56,010 --> 00:01:58,876 It adds the length and width together, giving us 6, and 34 00:01:58,876 --> 00:02:02,294 then multiplies the result by 2, giving us a return value of 12. 35 00:02:06,034 --> 00:02:10,054 Now this is one way to do it but this is actually a little longer than necessary. 36 00:02:10,054 --> 00:02:12,114 It's not Idiomatic Ruby. 37 00:02:12,114 --> 00:02:16,912 We can shorten this up by simply returning 38 00:02:16,912 --> 00:02:21,190 the result of the math operations. 39 00:02:21,190 --> 00:02:25,200 So we can get rid of the variable that we're storing everything in, and 40 00:02:25,200 --> 00:02:30,150 just return the results of the math operation. 41 00:02:30,150 --> 00:02:32,210 Let's try saving this and running it again. 42 00:02:33,380 --> 00:02:34,569 And you see we get the same results. 43 00:02:38,089 --> 00:02:41,440 But we can actually make it even shorter than that. 44 00:02:41,440 --> 00:02:45,190 The last expression that gets evaluated within the body 45 00:02:45,190 --> 00:02:49,640 of a Ruby method becomes the return value of that method. 46 00:02:49,640 --> 00:02:53,050 So we can actually take the return keyword off here. 47 00:02:53,050 --> 00:02:57,500 Length times width will be the last expression we evaluate within the area 48 00:02:57,500 --> 00:03:02,060 method, and so this will return the length and the width that were passed in. 49 00:03:03,410 --> 00:03:05,420 We can do the same down here in perimeter, 50 00:03:05,420 --> 00:03:09,600 the last expression that we evaluate here is 2 times length plus width. 51 00:03:10,760 --> 00:03:14,070 And that just becomes the return value of the perimeter method. 52 00:03:14,070 --> 00:03:15,850 So let's save this, try running it again. 53 00:03:18,470 --> 00:03:19,670 And again we get the same result. 54 00:03:20,950 --> 00:03:24,404 This is a much more succinct way to write methods in Ruby. 55 00:03:24,404 --> 00:03:25,795 And if your methods are short, 56 00:03:25,795 --> 00:03:28,750 you should consider leaving off the return keyword altogether. 57 00:03:30,180 --> 00:03:32,180 I hope you got in some good Ruby practice. 58 00:03:32,180 --> 00:03:35,040 See the teacher's notes for some other experiments you might try. 59 00:03:35,040 --> 00:03:35,540 Have fun!