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