1 00:00:00,246 --> 00:00:02,910 We can call a Length method on a string and 2 00:00:02,910 --> 00:00:05,590 we can call an even method on an integer. 3 00:00:05,590 --> 00:00:08,650 One thing we can't do is call the length method on an integer. 4 00:00:10,470 --> 00:00:14,150 Let's save that and try running it. 5 00:00:17,553 --> 00:00:22,450 We get the error undefined method length for Fixnum. 6 00:00:22,450 --> 00:00:24,920 We also can't call the even method on a string. 7 00:00:27,490 --> 00:00:28,757 Save that and try running it. 8 00:00:31,332 --> 00:00:35,980 This time we get the error undefined method even for a String. 9 00:00:35,980 --> 00:00:38,500 Not only did those operations not make sense, 10 00:00:38,500 --> 00:00:40,950 the methods don't exist on those objects. 11 00:00:40,950 --> 00:00:44,540 A string has a length method but not an even method and 12 00:00:44,540 --> 00:00:47,880 an integer has an even method but not a length method. 13 00:00:47,880 --> 00:00:52,720 We can get a list of the methods available on an object by calling a method named 14 00:00:52,720 --> 00:00:54,370 methods on it. 15 00:00:54,370 --> 00:00:57,090 so let's try that now with an integer object. 16 00:00:57,090 --> 00:01:02,010 We'll call pita printout result and we'll say 2.methods and 17 00:01:02,010 --> 00:01:05,780 we'll also chain a call to a method named sort on the result, so 18 00:01:05,780 --> 00:01:08,660 that the methods are sorted in alphabetical order. 19 00:01:08,660 --> 00:01:09,490 Let's try running that. 20 00:01:11,600 --> 00:01:14,300 And you can see, a large list of methods get returned. 21 00:01:16,690 --> 00:01:18,884 Let's do the same with the string. 22 00:01:18,884 --> 00:01:26,020 We'll call p AA .methods.sort, save that, try running it. 23 00:01:28,130 --> 00:01:31,030 And again, you can see the large list of methods get returned but 24 00:01:31,030 --> 00:01:32,520 they're different methods this time. 25 00:01:33,960 --> 00:01:37,430 So what decides which methods are available on an object? 26 00:01:37,430 --> 00:01:39,410 It's the object's class. 27 00:01:39,410 --> 00:01:43,530 You can call the class method on any object to find out what it's class is. 28 00:01:43,530 --> 00:01:49,712 So let's try printing AA.class and let's try printing 2.class. 29 00:01:53,024 --> 00:01:57,004 And if we run that, we see that we get String for the string and 30 00:01:57,004 --> 00:02:00,310 a class called Fixnum for the integer. 31 00:02:00,310 --> 00:02:02,560 You can think of a class as a blueprint. 32 00:02:02,560 --> 00:02:07,890 It's like a set of plans for building say, a car or a radio, or a house. 33 00:02:07,890 --> 00:02:10,980 Objects are constructed using the blueprint. 34 00:02:10,980 --> 00:02:14,030 The blueprint says how the objects should be structured and 35 00:02:14,030 --> 00:02:15,610 what they should be able to do. 36 00:02:15,610 --> 00:02:19,890 But the blueprint doesn't get into specific details about an individual car, 37 00:02:19,890 --> 00:02:22,290 radio or house, like their color. 38 00:02:22,290 --> 00:02:26,150 Likewise, a Ruby class specifies a new type of data. 39 00:02:26,150 --> 00:02:32,360 Objects are then constructed using that class, we call these instances of a class. 40 00:02:32,360 --> 00:02:35,121 The class specifies how the data should be stored and 41 00:02:35,121 --> 00:02:37,830 the things you should be able to do using that data. 42 00:02:37,830 --> 00:02:41,943 A class doesn't specify anything about its individual instances though, 43 00:02:41,943 --> 00:02:46,230 like the data they hold, that's left up to the objects themselves. 44 00:02:46,230 --> 00:02:50,780 You already know how to create objects from the string and fixed num classes. 45 00:02:50,780 --> 00:02:55,120 Just include text between quotation marks in your code and you'll have a string. 46 00:02:55,120 --> 00:02:59,210 Likewise, just include a whole number in your code and you'll have a fixed num. 47 00:02:59,210 --> 00:03:00,150 For other classes, 48 00:03:00,150 --> 00:03:03,860 you usually have to call a method on the class to get an instance of it. 49 00:03:03,860 --> 00:03:08,690 For example, earlier we briefly showed how to get an instance of the time class which 50 00:03:08,690 --> 00:03:13,800 represents the current time by calling the Time.now method. 51 00:03:13,800 --> 00:03:18,669 We can assign that new time object to a variable by saying time equals Time.now, 52 00:03:18,669 --> 00:03:21,972 we can name the variable whatever we want, of course. 53 00:03:21,972 --> 00:03:29,283 And if we print out its class by calling the class method on it. 54 00:03:29,283 --> 00:03:36,090 Let's try running that, we'll see that the result is an instance of the Time class. 55 00:03:36,090 --> 00:03:39,010 Now as I mentioned, you can get a list of the names of methods on 56 00:03:39,010 --> 00:03:41,600 an object by calling its methods, method. 57 00:03:43,640 --> 00:03:49,163 So if we create a string and call methods on it and then print the result out. 58 00:03:52,275 --> 00:03:56,076 We'll see a big list of methods available on it but that doesn't tell you what 59 00:03:56,076 --> 00:04:00,860 the methods do, what arguments they take, what their return values are, or anything. 60 00:04:00,860 --> 00:04:04,780 To learn all that, you'll need to look at the classes documentation. 61 00:04:04,780 --> 00:04:07,940 There's lots of great Ruby documentation sites out on the web. 62 00:04:07,940 --> 00:04:09,610 We just need to look them up. 63 00:04:09,610 --> 00:04:13,210 First, we need to be sure what class we're looking for documentation for. 64 00:04:13,210 --> 00:04:16,030 You can confirm that by calling the class method 65 00:04:16,030 --> 00:04:17,550 on the object that you want to look at. 66 00:04:18,630 --> 00:04:20,810 So, of course, this is a string. 67 00:04:20,810 --> 00:04:23,700 The class method returns string for this object. 68 00:04:23,700 --> 00:04:26,880 So, I know I need documentation for the string class. 69 00:04:26,880 --> 00:04:31,030 The next step is to look for that class in a web search engine. 70 00:04:31,030 --> 00:04:33,990 But I can't just type string into the search engine, 71 00:04:33,990 --> 00:04:38,490 that'll bring up a lot of results for similar classes in other languages. 72 00:04:38,490 --> 00:04:40,990 Instead I need to type ruby string. 73 00:04:43,840 --> 00:04:48,240 That will limit the results to pages that also contain the word ruby. 74 00:04:48,240 --> 00:04:50,600 The top result is for Class String. 75 00:04:50,600 --> 00:04:53,260 That looks promising, so I'll click on it. 76 00:04:53,260 --> 00:04:57,430 The methods available on a string object are listed down the side of the page. 77 00:04:57,430 --> 00:05:00,720 Each is a link to documentation for that method within the page. 78 00:05:03,450 --> 00:05:05,120 Let's click on reverse. 79 00:05:05,120 --> 00:05:09,090 It looks like it returns a new copy of the string but turned around backwards. 80 00:05:09,090 --> 00:05:10,650 That sounds like fun, so let's try it. 81 00:05:11,970 --> 00:05:13,600 We'll go back to our program. 82 00:05:15,270 --> 00:05:19,190 And we'll create a string object and call reverse on it. 83 00:05:20,930 --> 00:05:24,970 And that returns a string, so we'll need to call puts to print the result out. 84 00:05:24,970 --> 00:05:25,943 Let's try running this. 85 00:05:27,596 --> 00:05:31,660 And we get the string olleh or hello reversed. 86 00:05:31,660 --> 00:05:33,706 Let's see what else is in the documentation, 87 00:05:36,602 --> 00:05:40,488 Here's the length method that we were calling earlier, it says that it returns 88 00:05:40,488 --> 00:05:43,330 the length of a string in characters which we know is true. 89 00:05:45,458 --> 00:05:50,480 And here, near at the top of the list is a chomp method, let's see what that does. 90 00:05:50,480 --> 00:05:53,180 It looks like it returns a new copy of the string but 91 00:05:53,180 --> 00:05:55,500 with new line characters removed. 92 00:05:55,500 --> 00:05:59,230 That should help us with our widget store program because we still need to remove 93 00:05:59,230 --> 00:06:02,170 the new line character at the end of the user's keyboard input.