ruby

Aug 24 17:27

Ruby "between" method

Just discovered this today, does it even need an explanation for why it's cool?

4.between?(1,5) => true
4.between?(5,10) => false

No more code that looks like:
if value > 1 and value < 5
 yadda yadda
end

It'll simply be written like so:
if value.between?(1,5)
 yadda yadda
end

Dec 31 17:29

Converting a boolean to a yes or no in Rails

Every once in awhile I run across some syntactical sugar that makes me smile.

I have a boolean in a model for a project at work. I want the users to see a simple yes or no, not a true or false. I've struggled with how to do this in a dry, simple approach and have come up with several ideas. So today I found myself with this problem again, and not liking any of my previous solutions I asked Google for some ideas. I found this bit and put it in the model.

Note sla_flag is the boolean column in the DB.

def sla?
 self.sla_flag ? "Yes" : "No"
end

So now in my views I just reference "sla?".

I love that it doesn't require a multi-line if block. I love that it's a one line method.

I found this nugget here:
http://www.techlists.org/archives/programming/railslist/2006-07/msg03506...

Note the answer is from Yehuda Katz. I saw him at acts_as_conference last year and heard people talking about what an amazing Rubyist he is. When a simple answer like this can teach me so much about Ruby, I have to stop and shake my head. Despite my co-workers high regard for my Ruby skills, I am such a poser next to guys like Yehuda. I'm glad there are guys like him around to learn from!

This solution isn't the most dry, I'd rather make some sort of helper to handle this so that I don't have to do this in every model with a boolean. In spite of this, I have left it in the model. If I put it in a helper I'll never run across it again. So for now it's in plain view in the model to remind me of this syntax for future solutions. During some re-factoring session at a later date I'm sure I'll move it into a generic helper, or explore some of these ideas people have of extending the True class.

Jul 25 15:14

Shuffling my collection of girls

Yes, this is a dev post, no, it is not a post about my personal life. I'm happily married to only one woman. ;-)

Now that I've clarified that. If you've ever looked at HairForecast.com, you'll note that I have some graphics that depict the weather, and there's a different girl in each graphic. The girls wear coats when it's cold, they have umbrellas when it rains, their hair is blown around when it's windy, etc. You'll also note that I have 3 different girls that I rotate. I wanted to have the girls appear in random order, but never have the same girl appear twice (on the same forecast day).

Jul 10 13:42

Simplifying your test code

Robby, one of the developers I worked along side at BlueSpire, used a pattern in our test suite to simplify the setup of objects under test. I thought it very useful and it made the code so much more readable.

The idea is, when setting up an object n different ways, it's nice to set only the parameters you need on the same line as the instantiation.

Jul 08 22:18

Using method_missing in Ruby

I first learned about method_missing when Christopher Bennage asked me about it. Even after looking it up and explaining it to Christopher, I didn't find any practical use for it in the things I was doing.

At least until tonight, that is.

I've been working on expanding Hair Forecast into Canada. I have the Canadian weather data described in active record like so:

    create_table :canadaforecasts do |t|
      t.column :fcstdate, :date
      t.column :lat, :float
      t.column :lon, :float
    end

Jun 09 16:14

Simple templates in RHTML

I'm creating a very simple site (4 static pages) for some friends and didn't need to do anything fancy (yet), so I elected to use plain HTML and Javascript and not use a framework (like Rails). I like Rails, but I just can't see myself deploying the whole Rails tree just for a 4 page static site.