Making Time#to_date public in Ruby 1.8.6

I just got caught out by an upgrade to Ruby 1.8.6 and an app which I can’t as yet upgrade to Rails 1.2.3. Ruby 1.8.6 (and 1.9) make the Time#to_date and Time#to_datetime methods private so any code which depends on those methods being public will break.

The fix is very easy. I just copied the code from the relevant rails changeset and placed it in my environment.rb file:

class Time
  %w(to_date to_datetime).each do |method|
    public method if private_instance_methods.include?(method)
  end
end

Of course, the ideal is to upgrade to 1.2.3, but that’s not on the cards just yet for this particular project. In the meantime, having to add five lines isn’t too much trouble.

Tags: , ,

1 comment

  1. That’s an odd one. I’m curious to find out about the motivation behind the change (in Ruby).