Playing With Ruby Dates
One of my previous projects involved a ton of work using Flash’s built in Date object. We ended up rolling our own Calendar library which was both tedious and time consuming to debug. Now that I’m digging into Ruby for my newest project, I wanted to see what features the language has to offer. So far I’m really impressed and that’s after only a few hours of exploration. I’ll detail some of the tricks I’ve learned along the way so hopefully other newcomers can benefit.
Ruby Date Object Basics
We can start off by firing up IRB and requiring the date
class. Let’s do a really simple example first and just generate today.
Now lets try a bit of Ruby’s sugar to generate tomorrow’s date.
Pretty straightforward, right? Since there is usually more than one way to do something in Ruby we could have achieved the same results using any of the following.
As someone on StackOverflow pointed out: Date
objects are also Comparable
, so you can construct a Range
. If you wanted to collect every day from the previous week into an array you could do the following:
or…
There are also some cute booleans tossed into the mix for figuring out the day of the week.
How to Use Chronic
Chronic is a Ruby natural language date/time parser written by Tom Preston-Werner (@mojombo) which takes surprisingly human readable text and converts it to dates.
Covering everything that Chronic supports could take a while so definitely go check out the docs. Below is just a quick example to demonstrate how cool it is.
How to Use Active Support for Dates
Active Support is a library extracted from Rails which adds a ton of sugar to the Ruby language. As the author’s describe it:
Active Support is a collection of various utility classes and standard library extensions that were found useful for Rails. All these additions have hence been collected in this bundle as a way to gather all that sugar that makes Ruby sweeter.
It’s broken into several pieces so you can choose to load only the parts that you’ll actually be using. I’m going to write an upcoming article on Active Support. For now we’ll just require it all.
You’ll notice that Active Support has changed the way our date’s to_s
is formatted so it’s more human readable. It also added shortcuts for creating Dates on either side of today.
Included as well are some nice convenience booleans: past?
, today?
, and future?
If you’ve ever had to write a Calendar that can support weeks, especially those that straddle two different months, you’ll appreciate the simplicity of the helpers Active Support adds.
We aren’t limited to weeks though. Active Support adds methods for days, months and years. For example:
By extending FixNum
to support additional Date methods certain operations become much more readable.
These extensions are referred to as durations
in the documentation.
Which brings us back to one of our first examples of finding the date 7 days ago. With Active Support it’s as easy as…
Pretty cool! Active Support adds A LOT more than just Date helpers and I’ll try to cover it more in some future articles. Definitely check out the documentation (you can skip to the Date section since it’s pretty immense).
Source:
http://stackoverflow.com/questions/962544/one-line-hash-creation-in-ruby
http://www.developer.com/open/article.php/3729206/Telling-Time-with-Ruby.htm
http://guides.rubyonrails.org/active_support_core_extensions.html
You should follow me on Twitter here.