1 00:00:00,000 --> 00:00:04,950 [MUSIC] 2 00:00:04,950 --> 00:00:08,347 Let's practice some basic Ruby to make sure that everything you've learned so 3 00:00:08,347 --> 00:00:09,200 far sticks. 4 00:00:09,200 --> 00:00:14,310 We'll review numeric types such as fix, num and float, as well as math operations. 5 00:00:14,310 --> 00:00:18,040 We're gonna ask you to build a simple Ruby program that calculates the average of 6 00:00:18,040 --> 00:00:18,779 some numbers. 7 00:00:19,870 --> 00:00:23,400 I've attached a work space to this video which includes an average .rb 8 00:00:23,400 --> 00:00:26,520 starter file, with instructions as Ruby comments. 9 00:00:26,520 --> 00:00:27,830 You should open the work space now. 10 00:00:29,070 --> 00:00:32,670 If you have Ruby installed on your computer you can choose file, 11 00:00:32,670 --> 00:00:33,640 download work space. 12 00:00:33,640 --> 00:00:38,380 And that will download all the files to your 13 00:00:38,380 --> 00:00:40,690 computer where you can edit and run them. 14 00:00:40,690 --> 00:00:44,390 Or you can just set it and run everything in the work space. 15 00:00:44,390 --> 00:00:47,790 Write your code in the average.rb file and save your work. 16 00:00:47,790 --> 00:00:52,070 Then switch to the terminal and type ruby average.rb to run your program. 17 00:00:53,500 --> 00:00:56,200 Up here at the top of the file we've already defined four Ruby 18 00:00:56,200 --> 00:00:57,470 variables for you. 19 00:00:57,470 --> 00:01:00,110 Your job is to take the value of all those variables and 20 00:01:00,110 --> 00:01:02,530 calculate the average, that is the mean of them. 21 00:01:03,800 --> 00:01:06,220 You can do this by adding all their values together and 22 00:01:06,220 --> 00:01:10,050 then dividing by the number of values, that's four values in this case. 23 00:01:11,240 --> 00:01:14,830 Now there's a little catch to this, for the numbers 12, 7, 5 and 10, 24 00:01:14,830 --> 00:01:18,400 you should get an average of 8.5, not 8. 25 00:01:18,400 --> 00:01:21,590 If you find that you got the number of 8 even, 26 00:01:21,590 --> 00:01:24,730 it's probably because you divided by a fixed sum, an integer. 27 00:01:24,730 --> 00:01:27,480 So make sure that you're dividing by a float instead. 28 00:01:27,480 --> 00:01:29,170 See the teacher's notes for 29 00:01:29,170 --> 00:01:33,110 videos that can help you fix this problem if it arises. 30 00:01:33,110 --> 00:01:35,720 There's an extra credit problem down here at the bottom you can try. 31 00:01:35,720 --> 00:01:40,679 You can prompt the user for some values to average by populating the E, B, C and 32 00:01:40,679 --> 00:01:44,200 D variables by calling the get s method. 33 00:01:44,200 --> 00:01:47,620 Note however, that the get s method returns the string not a number. 34 00:01:47,620 --> 00:01:50,530 So you'll need to call the to_f method on the value 35 00:01:50,530 --> 00:01:53,320 returned by gets to convert it to a float. 36 00:01:53,320 --> 00:01:58,860 That is when we type gets.to_f and then store that value in a variable. 37 00:01:59,900 --> 00:02:03,422 Then you can calculate an average of all the variables as you did previously. 38 00:02:03,422 --> 00:02:07,810 I've linked to some videos in the teacher's notes which you can refer to 39 00:02:07,810 --> 00:02:09,060 if you get stuck. 40 00:02:09,060 --> 00:02:11,770 After you've written your program you can proceed to the next video 41 00:02:11,770 --> 00:02:14,670 where you can compare my solution to yours. 42 00:02:14,670 --> 00:02:15,260 Ready? 43 00:02:15,260 --> 00:02:16,660 Get started on your program. 44 00:02:16,660 --> 00:02:18,420 I'll show you my version and the next video.