Pop quiz: how do you get the month (as a name, not a number) of a Time in Ruby (without looking at the Time#strftime documentation)?

All I remembered was that it was some meaningless looking format string. Here’s my first guess:
1
2
irb(main):001:0> Time.now.strftime("%a")
=> "Tue"
Oops, it’s actually:
1
2
irb(main):002:0> Time.now.strftime("%b")
=> "Feb"

That doesn’t look very ruby-like. Aren’t % format strings for C programmers (whoever wrote the code I’m currently refactoring is probably a recovering C programmer. They even use sprintf…)?

How about:
1
2
 irb(main):003:0> Time.now.short_month_name
=> "Feb"

I’m sure others have done this already, but I couldn’t find it so I made my own Rails plugin, and stuck it here: http://svn.laurelfan.com/decorated_time/, mostly to see if I could set up an svn repository on dreamhost. The stuff that actually does the work is a total of about 4 lines of code since Ruby nicely lets me reopen the Time class at will.

(speaking of the day of week, I noticed that 1.9 added monday?, tuesday?, etc methods)

Sorry, comments are closed for this article.