back

Rails 2.2 String Inquirer caveat ( ActiveSupport::StringInquirer ), Part I

As of v. 2.2. Rails now lets us do things like Rails.env.production? instead of Rails.env == "production". The syntax is:

I initially read how you could call Rails.env.development? as well as Rails.env.test?, so I thought that I would be able to make interfaces like this:

However, it turns out that if I set the food to pillows and execute m.food.pillows? I get false. Using the syntax that is provided in the documentation (ActiveSupport::StringInquirer.new("basil")) only allows us to use food.basil? if we want to get back true. So the only time you can get back true would be if it were basil.

So, we can never call m.food.bread? and get back true, even if m.food == “bread” returned true!

Imo, this makes it not so usefull for practical application. Say I want to cleanup some code that for some reason cannot use case but must often do if m.food == "food x". Even with StringInquirer, I could not turn more than one of those statements into m.food.x?, because I can only use it one time. Oops.

For an in depth look and a solution, check out part II

December 09, 2008