Join Kevin Skoglund for an in-depth discussion in this video Date, part of Ruby: Classes and Modules.
- In this movie we're going to learn about Ruby's Date class. The Date class is going to be very similar to what we just saw in the last movie with the Time class. Now, the time includes the date, so you may be wondering, "Why do we need a Date class? "Why not just use the Time?" You certainly can. But sometimes it's just easier to work with Dates than Times. If we don't care about the time and the minutes, the seconds, we just want to focus on the date, it can be easier to use this class. But more importantly, the Date class includes additional methods the Time class doesn't have.
The Date class and all of it's additional methods are part of the Ruby Standard Library. What that means, is that when you install Ruby, the Date class is installed along with Ruby. But it's not automatically loaded. That means that you don't naturally have the ability to reference it or it's methods inside irb or your Ruby scripts. Instead, you have to first tell Ruby that you want to load it. That's because it's a commonly used library, but it is not automatically used. So Ruby just doesn't take the time to load in those extra classes unless you know that you need them.
If you go to the Ruby-Doc website, the core API is where you'll find information about all of the built-in and pre-loaded methods of Ruby. Standard library is where you'll find information about the standard library functions. Things that are included but not automatically loaded. And you'll see that Date is one that's listed right down here. If we go to the Date class, it looks just the same, all the documentation is the same, and you'll see all of the different methods that are allowed here. Things that are in addition to the Time class. We're going to be taking a look at some of those. So now that we understand how the Ruby Standard Library works, we need to be able to tell Ruby to load in that code so we can make use of it.
And we do that by just simply putting a line in irb, or in one of our Ruby scripts that says require, and then the name of that library. And that's just simply "date", all underscore. And that'll load in that code and make it available so we can use it. So, for example, we could have require "date", and then we would be able to call Date.today, and it would return back an instance of a date object. And then we could work with that object. For example, I can call "month" on it to find out the month number. We can also use Date.new, just like we could with time to create a new date by providing the year, the month, and the day.
We can also transform a day into a time. So what's actually happening here in this line of code, is that Date.today returns a date instance, and then we tell that date instance to call a method to underscore time, which then returns a time instance. That is, an instance from the Time class, like we were working with in the last movie. And we can do the same thing in reverse. If we have Time.now that the time instance, we can call to underscore date and it will convert that into a date instance, or an instance from the date class.
Once we have a date, there are a number of instance methods we can call on it that are additional. For example, leap?, will return true or false, whether this is a leap year. Cweek and cwday will return the calendar week or the calendar week day. Next day, next month, next year, are all available as methods that will just instantly tell us what the next month is, what the next year is, instead of having to do addition. And we can do the same thing with prev day, prev month, and prev year to get the previous versions. You can even ask whether something is Gregorian or Julian to find out if a date is in the period of time when those calendars applied.
So you can see there's a lot of extra features that are added in with Date that we don't get with Time. Let's try out a few of those in irb. So the first thing we've got to do is we want to do require date. We want to make sure we do that in irb every time, we want to use date, and we need to put it somewhere in our Ruby script before we try and call for a date object or a method on date. So date.today returns a date object. And date.today.month and you can see that it returns back the month. Same thing if we did year. Now as I mentioned we can take time.now and we can call to underscore date on it, that returns a date object.
You can actually confirm that by calling class on the end of that and see that it returns a class. Time.now of course, returns a time object. Let's try date.today. Let's do next year. You can see it returns a new date object, and I can ask it for what that year is, once it returns that object. We can find out is today a leap year? So date equals date.today. And then let's say date.leap, question mark, false.
Now if I do date.next_year.leap? Nope, that's also false. If I do the next year after that, finally it's true. That is going to be a leap year. You get the idea. The rule of thumb is that if you need something very simple, just use Time. But if you're working with, primarily, dates, or if you need these extra features, then load in the Date class from the Standard Library.
- Defining a custom class
- Working with instances of a class
- Class and instance attributes and methods
- Class inheritance
- Working with dates and times
- Using modules as namespaces and mixins
- Raising and handling exceptions