1 00:00:00,000 --> 00:00:04,865 [MUSIC] 2 00:00:04,865 --> 00:00:08,490 Our program always charges users \$10 per widget. 3 00:00:08,490 --> 00:00:11,430 It takes the number of widgets the user wants to order and 4 00:00:11,430 --> 00:00:14,780 multiplies it by ten to get the total cost. 5 00:00:14,780 --> 00:00:19,030 Our last requirement is to provide a discount for large orders of widgets. 6 00:00:19,030 --> 00:00:21,720 Specifically, if the order is for 100 widgets or 7 00:00:21,720 --> 00:00:24,580 more, the price should be \$8 per widget. 8 00:00:24,580 --> 00:00:28,660 If it's for 50 widgets or more, the price should be \$9 per widget. 9 00:00:28,660 --> 00:00:32,795 And all other orders should be the full price, \$10 per widget. 10 00:00:32,795 --> 00:00:36,656 Our first problem here is to figure out which discount we should apply. 11 00:00:36,656 --> 00:00:38,949 Are they ordering 100 widgets or more? 12 00:00:38,949 --> 00:00:41,990 50 to 99, less than 50? 13 00:00:41,990 --> 00:00:45,149 To do this, we're going to need a way to compare numbers. 14 00:00:46,510 --> 00:00:51,020 This code will be a little too complicated to embed within a string, so first, 15 00:00:51,020 --> 00:00:53,960 let's move the part that calculates a price out to a method. 16 00:00:55,190 --> 00:00:58,210 We'll define a method named price. 17 00:00:59,860 --> 00:01:08,038 And we'll take the quantity that we are calculating a price for as a parameter. 18 00:01:11,483 --> 00:01:14,210 Type the end keyword to end the method. 19 00:01:14,210 --> 00:01:21,070 And here in the method body, we'll simply return the value quantity * 10. 20 00:01:21,070 --> 00:01:24,940 We're going to assume that the quantity's already been converted to an integer 21 00:01:24,940 --> 00:01:26,900 before the method is called. 22 00:01:26,900 --> 00:01:29,570 So we'll simply return the quantity times 10. 23 00:01:29,570 --> 00:01:32,720 And now we need to change our main code down here to call that method. 24 00:01:32,720 --> 00:01:36,570 So we'll say total =, we'll call price, and 25 00:01:36,570 --> 00:01:40,460 we'll pass it the number that our user entered. 26 00:01:40,460 --> 00:01:43,667 Then here, instead of saying number * 10, 27 00:01:43,667 --> 00:01:47,625 we'll just put the results of calling the price method. 28 00:01:47,625 --> 00:01:52,648 Let's try running this, and we should get the same result when there's 7 widgets, 29 00:01:52,648 --> 00:01:55,033 and for 7 widgets your total is \$70. 30 00:01:55,033 --> 00:01:59,568 But our calculation is being routed through the price method this time, 31 00:01:59,568 --> 00:02:02,855 instead of directly interpolated into the string. 32 00:02:02,855 --> 00:02:07,481 In order to know which discount to apply, we need to be able to tell if the order 33 00:02:07,481 --> 00:02:12,560 quantity is over 100, if it's between 50 and 100, or if it's below 50. 34 00:02:12,560 --> 00:02:17,630 Ruby uses comparison operators to tell whether one value is equal to, 35 00:02:17,630 --> 00:02:19,610 greater than, or less than another. 36 00:02:20,650 --> 00:02:23,940 Comparison operators return a boolean value. 37 00:02:23,940 --> 00:02:27,010 Boolean values are either true or false. 38 00:02:27,010 --> 00:02:31,390 They're represented in Ruby code by the words true and false. 39 00:02:31,390 --> 00:02:35,470 Note that these are not strings, so they're not surrounded by quotes. 40 00:02:35,470 --> 00:02:38,530 Let's try out some of these comparison operators in IRB. 41 00:02:40,110 --> 00:02:42,990 So first we're going to create a variable, we'll call it quantity. 42 00:02:44,250 --> 00:02:46,940 And we'll assign the variable 75 to it. 43 00:02:46,940 --> 00:02:50,520 Now, let's test the value and quantity to see if it's greater than 50. 44 00:02:50,520 --> 00:02:53,460 quantity > 50. 45 00:02:53,460 --> 00:02:57,900 We get the value true because, of course, 75 is greater than 50. 46 00:02:57,900 --> 00:03:00,260 Let's test whether it's less than 50. 47 00:03:00,260 --> 00:03:03,200 quantity < 50. 48 00:03:03,200 --> 00:03:05,570 That, of course, is false. 49 00:03:05,570 --> 00:03:08,150 Now, let's try the equality comparison operator. 50 00:03:08,150 --> 00:03:13,440 We'll test whether quantity is equal to 75. 51 00:03:13,440 --> 00:03:16,560 We get true because, of course, it is. 52 00:03:16,560 --> 00:03:21,220 Note that it's important not to confuse the double equals comparison operator with 53 00:03:21,220 --> 00:03:24,150 the single equals assignment operator. 54 00:03:24,150 --> 00:03:31,550 For example, if we try typing quantity == 99, we get false. 55 00:03:31,550 --> 00:03:35,587 Because it compared the value that quantity already held with the number 99, 56 00:03:35,587 --> 00:03:37,525 and of course those two are not equal. 57 00:03:37,525 --> 00:03:39,925 If we take a look at the quantity variable, 58 00:03:39,925 --> 00:03:42,932 we can see that it was unaffected by that comparison. 59 00:03:42,932 --> 00:03:48,107 But if we were to accidentally type quantity = 99, 60 00:03:48,107 --> 00:03:54,920 the result would be the number 99, which Ruby treats as true. 61 00:03:54,920 --> 00:03:59,660 So not only do you mistakenly get a true value for your comparison, but 62 00:03:59,660 --> 00:04:04,410 you've overwritten the value that's in the quantity variable. 63 00:04:04,410 --> 00:04:09,220 So be very sure not to confuse the double equals comparison operator with the single 64 00:04:09,220 --> 00:04:11,480 equals assignment operator. 65 00:04:11,480 --> 00:04:14,360 Now let's try out the greater than or equal to and the less than or 66 00:04:14,360 --> 00:04:15,640 equal to operators. 67 00:04:17,040 --> 00:04:21,375 Now remember that the quantity variable currently contains the value 99. 68 00:04:21,375 --> 00:04:24,703 Let's see whether it's greater than or equal to the value 50. 69 00:04:29,172 --> 00:04:31,327 That, of course, is true. 70 00:04:31,327 --> 00:04:33,439 Let's see if it's less than or equal to 50. 71 00:04:38,271 --> 00:04:39,615 That's false. 72 00:04:39,615 --> 00:04:42,763 If we assign the value 50 to quantity, 73 00:04:42,763 --> 00:04:46,663 then both of those comparisons will return true. 74 00:04:49,821 --> 00:04:52,440 Is quantity greater than or equal to 50? 75 00:04:52,440 --> 00:04:54,110 Yes, that's true. 76 00:04:54,110 --> 00:04:56,950 Is quantity less than or equal to 50? 77 00:04:56,950 --> 00:05:01,420 That's also true because quantity is equal to 50. 78 00:05:01,420 --> 00:05:05,068 There's one more comparison operator you should know, and 79 00:05:05,068 --> 00:05:07,151 that's the not equals operator. 80 00:05:07,151 --> 00:05:09,945 Quantity != 50. 81 00:05:09,945 --> 00:05:13,036 The exclamation point can be read aloud as not, so 82 00:05:13,036 --> 00:05:17,500 exclamation point equals sign can be read aloud as not equal. 83 00:05:17,500 --> 00:05:20,440 Since quantity is currently set to the value 50, 84 00:05:20,440 --> 00:05:24,250 quantity not equals 50 is going to return false. 85 00:05:24,250 --> 00:05:27,740 The not equal comparison operator can be thought of as the opposite 86 00:05:27,740 --> 00:05:30,240 of the double equals comparison operator. 87 00:05:33,860 --> 00:05:36,530 Which in this case, of course, return true. 88 00:05:36,530 --> 00:05:40,305 All the comparison operators we just showed you work with strings as well. 89 00:05:40,305 --> 00:05:46,702 So let's try creating a variable named string, we'll assign it the string zebra. 90 00:05:46,702 --> 00:05:49,753 And now let's try comparing that value to the string ant. 91 00:05:49,753 --> 00:05:52,440 So we'll say string, is it less than ant? 92 00:05:53,820 --> 00:05:58,960 We get the value false because z comes after a in the alphabet and 93 00:05:58,960 --> 00:06:02,020 is therefore treated as a greater value. 94 00:06:02,020 --> 00:06:05,530 So let's try a greater than comparison, is string greater than ant? 95 00:06:07,070 --> 00:06:08,810 That returns true. 96 00:06:08,810 --> 00:06:10,220 Is string equal to ant? 97 00:06:13,240 --> 00:06:16,497 Well, no, that's not what it holds, so that returns false. 98 00:06:16,497 --> 00:06:18,908 Is string equal to zebra? 99 00:06:21,777 --> 00:06:23,630 That returns true. 100 00:06:23,630 --> 00:06:28,130 Is string equal to ZEBRA in all caps? 101 00:06:30,000 --> 00:06:32,780 Well, no, one is capitalized and the other is lowercase. 102 00:06:32,780 --> 00:06:33,790 So they're not equal. 103 00:06:34,890 --> 00:06:37,285 Is strong not equal to ant? 104 00:06:39,270 --> 00:06:43,130 Well yes, the two strings are different and therefore they're not equal. 105 00:06:43,130 --> 00:06:44,699 And it's not just strings. 106 00:06:44,699 --> 00:06:48,153 Although you use them most often with strings and numbers, 107 00:06:48,153 --> 00:06:51,415 comparison operators work with almost any Ruby class. 108 00:06:51,415 --> 00:06:54,108 Let's create a variable named time and 109 00:06:54,108 --> 00:06:57,380 assign it the results of the Time.now method. 110 00:06:59,160 --> 00:07:04,278 and now let's test whether time is less than another call to Time.now. 111 00:07:04,278 --> 00:07:09,105 That's true because the value in the variable time is a few seconds in the past 112 00:07:09,105 --> 00:07:11,792 and therefore it's less than Time.now. 113 00:07:11,792 --> 00:07:17,230 Now let's test whether time is greater than Time.now. 114 00:07:17,230 --> 00:07:19,540 That of course is false. 115 00:07:19,540 --> 00:07:23,100 Make sure you don't try to compare values that are of two different classes, 116 00:07:23,100 --> 00:07:24,320 because that won't work. 117 00:07:24,320 --> 00:07:28,907 Let's take the string 3, and test whether it's equal to the number 3. 118 00:07:28,907 --> 00:07:33,612 The equality comparison operator will always return false if you're comparing 119 00:07:33,612 --> 00:07:35,627 values of two different classes. 120 00:07:35,627 --> 00:07:38,591 If you try to use a greater than or less than comparison, 121 00:07:38,591 --> 00:07:40,053 that will return an error. 122 00:07:40,053 --> 00:07:45,040 Let's see see whether the string 4 is greater than the number 3. 123 00:07:45,040 --> 00:07:47,570 We get an actual error for that. 124 00:07:47,570 --> 00:07:51,310 So if you're going to compare values, make sure they're of the same class. 125 00:07:51,310 --> 00:07:54,880 By the way, all the comparison operators we just showed you work in pretty much 126 00:07:54,880 --> 00:07:56,650 every programming language out there. 127 00:07:56,650 --> 00:07:58,930 So remember them because you'll be using them a lot.