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…

''

About thoughtbot

We've been helping engineering teams deliver exceptional products for over 20 years. Our designers, developers, and product managers work closely with teams to solve your toughest software challenges through collaborative design and development. Learn more about us.