TheDelChop
TheDelChop

Reputation: 7998

More Ruby-esque way to code this?

I've got a method I'm calling expired? which is simply meant to check to see if its been more than 24 hours since my object has been updated, and if so return true. Here is my method, but although it works, it feels dirty, can anybody think of a better way to achieve my goal?

DAY_IN_SECS = 86400

def expired?
  return true unless (Time.now <=> self.updated_at + DAY_IN_SECS) == -1
end

Upvotes: 1

Views: 418

Answers (3)

DigitalRoss
DigitalRoss

Reputation: 146053

Even in plain Ruby you could still do the simpler:

def expired?
  Time.now > self.updated_at + DAY_IN_SECS
end

Upvotes: 5

Nicolas Blanco
Nicolas Blanco

Reputation: 11299

You're working with Rails, so you can use Rails Date/Time methods :).

For your problem, I like this :

def expired?
  updated_at.advance(:days => 1).past?
end

Upvotes: 3

William
William

Reputation: 3529

You could use the active_support functionality, so:

def expired?
  self.updated_at > 1.day.ago
end

Upvotes: 12

Related Questions