Welcome to Giant Robots Smashing Into Other Giant Robots — a weblog about development, business, design and technology — written by thoughtbot.

mas o menos

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

1
2
3
4
5
6
7
8
9
10
11
  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.

1
2
3
4
5
6
7
  def after?(other)
    self > other
  end

  def before?(other)
    self < other
  end
1
2
3
4
5
6
7
8
9
10
11
  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 this entry

 

thoughtbot is hiring

We are hiring web developers and web designers in both Boston and New York, NY.

What are we up to?

We built Shoulda, an eclectic set of additions to Test::Unit; Paperclip to manage uploaded files without hassle; Jester, a REST/ActiveResource client library written in Javascript, and Squirrel, an enhancement for ActiveRecord's find syntax; — amongst some other projects.


Chad (President) and Jon (CTO) co-authored a technical book titled Pro Active Record: Databases with Ruby and Rails, which explores the ins and outs of the ActiveRecord ruby library. You can buy it today at Amazon.com.

About thoughtbot, inc.

We are a small web application development consulting business, with offices in Boston, MA and New York, NY. If you're looking to find a team for your next web development project or your new web application — get in touch.