taelor
taelor

Reputation: 639

Is this a dangerous override for Date.today in Ruby (on Rails)? (Timezone)

class Date
  class <<self
    alias_method :broke_ass_today, :today
  end

  def self.today
    Time.zone.now.to_date rescue Date.broke_ass_today
  end
end

because I would really hate to replace Date.today with that statement everywhere in our code base...

that and its just much simpler write (and read) date Date.today, because that's what we are used to.

Upvotes: 1

Views: 521

Answers (2)

Pete Brumm
Pete Brumm

Reputation: 1696

Date.current uses Time.zone so you can use that instead.

Upvotes: 0

socjopata
socjopata

Reputation: 5095

That sounds like begging yourself for difficult to debug errors, also new developers on this project may scratch their heads for a while if you forget to inform them about this little enhancement ;)

There are tools that will allow you to replace each found "Date.today" in your code, with your custom written method, in just few seconds. And it's not hard to do. Maybe you should consider that option? For me at least, expanding Date class with your custom method sounds better that overwriting today method.

Upvotes: 1

Related Questions