开发者

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 b开发者_如何学Goetter way to achieve my goal?

DAY_IN_SECS = 86400

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


You could use the active_support functionality, so:

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


Even in plain Ruby you could still do the simpler:

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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜