mas o menos

Jared Carroll

Everyone seems to trip up when it comes to #< and #> and dates and times.

def recent
  if published_on > 1.week.ago
    # blah
  end
end

def old
  if published_on < 1.week.ago
    # blah
  end
end

Nobody thinks of dates and times as numbers, so its hard to do comparisons on them quickly like you would with numbers. So lets add 2 methods to our dates and times.

def after?(other)
  self > other
end

def before?(other)
  self < other
end

def recent
  if published_on.after?(1.week.ago)
    # blah
  end
end

def old
  if published_on.before?(1.week.ago)
    # blah
  end
end

Luckily our good friends over at dzone have already a real solid impl of these 2 queries. LEFT_SIDE_LATER!

..in other news…

''

Jared Carroll

Pair with one of our expert developers to level up your skills with Coaching by thoughtbot. Save time learning best practices and techniques for reducing technical debt in Ember, Ruby, Haskell, and Go in 1-on-1 sessions tailored to your goals.