1 00:00:00,000 --> 00:00:04,640 [MUSIC] 2 00:00:04,640 --> 00:00:10,120 Programming languages use strings a lot, but they use numbers even more often. 3 00:00:10,120 --> 00:00:13,960 Numbers in Ruby can be used in all the math operations that other programming 4 00:00:13,960 --> 00:00:15,380 languages support. 5 00:00:15,380 --> 00:00:20,490 But unlike in most other languages, numbers in Ruby are also objects. 6 00:00:20,490 --> 00:00:23,360 That means they have lots of useful methods attached to them, 7 00:00:23,360 --> 00:00:24,530 just like with strings. 8 00:00:25,770 --> 00:00:30,220 Here we have some code that assigns the integer 12 to a variable 9 00:00:30,220 --> 00:00:31,700 named whole_number, and 10 00:00:31,700 --> 00:00:35,970 the fractional number 12.34 to a variable named fractional_number. 11 00:00:35,970 --> 00:00:40,020 That dot within the numbers is important, because it decides which of Ruby's 12 00:00:40,020 --> 00:00:43,990 two most common numeric classes the number gets assigned to. 13 00:00:43,990 --> 00:00:46,620 Let's print out the class for each of these numbers, so 14 00:00:46,620 --> 00:00:50,720 we'll call the class method on the value and 15 00:00:50,720 --> 00:00:54,180 store it in each of those variables, save that, try running it, 16 00:00:57,560 --> 00:01:02,320 The value in whole_number is a fixnum, which is a kind of integer. 17 00:01:02,320 --> 00:01:06,660 The value in fractional_number is a float, as in a floating point number. 18 00:01:08,550 --> 00:01:10,840 Let's try setting fractional_number to 12.0, and 19 00:01:10,840 --> 00:01:13,100 see if it's still treated as a float. 20 00:01:16,240 --> 00:01:17,380 It looks like it is. 21 00:01:17,380 --> 00:01:21,180 They key is whether the number contains a decimal place or not. 22 00:01:21,180 --> 00:01:25,120 The distinction between fixnum and float classes is important 23 00:01:25,120 --> 00:01:29,050 partly because it affects what methods are available on a number. 24 00:01:29,050 --> 00:01:32,520 Let's bring up a search engine and do a search for ruby fixnum. 25 00:01:34,530 --> 00:01:39,060 Just like before, one of the top hits will be a Ruby documentation site. 26 00:01:39,060 --> 00:01:43,960 Here on the side we see the even method that we've been calling previously. 27 00:01:45,050 --> 00:01:47,395 So let's try calling it on whole_number. 28 00:01:49,405 --> 00:01:56,650 Whole_number.even?, don't forget the question mark at the end, save that, 29 00:01:56,650 --> 00:02:02,140 and try running it, and we get the result true, because 12 is a whole number. 30 00:02:03,310 --> 00:02:05,979 For fun, let's try calling it on fractional_numbers as 31 00:02:05,979 --> 00:02:11,150 well, Dot even question mark. 32 00:02:13,790 --> 00:02:15,340 Save it, run it. 33 00:02:15,340 --> 00:02:19,450 The call to even works for whole_number, but it fails for fractional_number, 34 00:02:19,450 --> 00:02:22,850 because that method doesn't exist on the float class. 35 00:02:22,850 --> 00:02:23,650 So remember, 36 00:02:23,650 --> 00:02:27,790 it's important to know whether a number in Ruby is a fixnum or a float. 37 00:02:27,790 --> 00:02:30,910 If the number in your code has a decimal point, it's a float. 38 00:02:30,910 --> 00:02:32,370 Otherwise, it's a fixnum.