The Ruby Standard Library5:11 with Jason Seifer
The Ruby Standard Library contains larger pieces of functionality and must be specifically imported in to your programs. Some examples of standard library are utility classes for working with dates and times, serialization, network access and more.
[MUSIC] 0:00 While Ruby Core contains modules and classes, 0:04 the Ruby Standard library contains larger pieces of functionality and 0:07 must be specifically imported into your programs. 0:12 Some examples of standard library are utility classes for working with dates and 0:16 times, serialization, network access, and more. 0:21 We could do a whole course on every piece of the standard library. 0:26 However, we're just going to look at some of the different pieces of functionality 0:30 available today. 0:34 What we see here is the Ruby Standard Library Documentation. 0:36 Now, the main panel says just a little bit about how the documentation was generated. 0:40 And gives thanks to the people who helped make it happen. 0:45 On the left side of the main documentation, 0:48 you'll find the table of contents. 0:51 This lists every available package in the standard library and is sorted by letter. 0:53 If we click into a package, we'll get the documentation format that we're used to 0:59 for other classes and modules. 1:04 Most documentation for items in the standard library will be split up 1:08 among different classes and modules. 1:12 This naming scheme is common in Ruby and encourages separation of concerns when 1:14 coding projects larger than just a few methods or files. 1:19 Right now, I've loaded up the documentation for CSV, which stands for 1:23 comma separated values. 1:28 This is a common format which seems easy to implement programmatically, but 1:30 is actually quite involved, which is why there's a library for it. 1:35 Now that we click into the standard library for 1:38 CSV, we'll see a bunch of different classes. 1:41 Most commonly, the documentation for 1:46 what we want will have the same name as the package. 1:48 In the CSV example, I'm gonna click on the top level CSV class again. 1:52 When I do that, we get a description of what the package does, 1:58 followed by some code examples of how it can work. 2:02 We can generally use these code examples 2:05 as a guide to accomplish what we want to in our programs. 2:08 On the left side, we have the usual method list that the class supports. 2:11 In larger packages, like we find in the standard library, we'll also see a name 2:16 space section which will show any other sub classes that this library has. 2:21 Finally, if we scroll down we can see any modules that are included with this class, 2:26 and also included in the library. 2:32 Some packages like this one for CSV may also have notes, 2:34 gotchas, warnings or other things to keep in mind when coding. 2:37 This one contains notes on how different character and codings are handled. 2:42 And also a note that we may find bugs in the code, 2:47 as well as where to report the issue. 2:50 When we get a little further into the documentation, we'll also see a list of 2:53 constants in the class before the rest of the methods are listed. 2:57 One thing that's interesting to note about packages in the standard library, 3:01 is that they have to be explicitly required. 3:04 For example, if I open up irb and try to access the CSV class, 3:08 I'll get an error saying that there's an uninitialized constant. 3:13 In order to use it, I'll have to explicitly require CSV. 3:17 Once I do that, I'll have access to the CSV class. 3:21 If I click back into the rest of the standard library documentation, 3:26 we can see that there's a wealth of functionality available to us. 3:30 The Benchmark module for 3:34 example, will allow us to measure the time it takes our code to run. 3:36 This can help us if we're trying to optimize different pieces of code, and 3:41 get an idea of where the bottlenecks might be in our programs. 3:45 The CGI library is larger library for working with low level web requests and 3:52 has several sub classes inside of it. 3:58 If we go back even further, the FileUtils module 4:02 gives us a platform agnostic way to work with files and directories. 4:06 We can do things like change and list directories, copy, move files, and more. 4:12 As we keep scrolling through the documentation for 4:19 the standard library, we'll see different sections. 4:21 For example, Net/HTTP, 4:25 the Net HTTP module can be used to consume APIs, and 4:29 also fetch and work with webpages programatically. 4:33 It's a good idea to read over the classes and libraries in the standard library. 4:37 By being familiar with the standard library, 4:41 you'll have a leg up on programming, and 4:43 not be tempted to re-implement these pieces of functionality on your own. 4:45 Some classes to be familiar with are forwardable, 4:49 observer, prettyprint, optparse, and more. 4:54 Using the documentation link below, 5:01 try reading through some of the standard library documentation now, and 5:03 practice with some of these classes on your own using workspaces. 5:07
You need to sign up for Treehouse in order to download course files.Sign up