I learned a new inflector method the other day, constantize. Here’s the method I used it in.

def allowed_to_read?(role)
  level = "Document::CAN_READ_" + self.readinglevel.upcase
  arr = level.constantize
  return true if "#{arr}".include? "#{role}"
end

I have an app where only certain people are allowed to read certain documents. So each user is assigned a role and then each document is assigned a group saying who is allowed to read it. I named the roles and readinglevels the same and then made a constant in the model that’s named, for example:

CAN_READ_ASSISTANT = [“assistant”, “associate”]

So when I’m checking if someone is allowed_to_read a document, I look at the document’s readinglevel and join it to the string “Document::CAN_READ_”. Then what I want to do is find out if the user’s role is included in the array named the same as the string I just created. That’s exactly what the constantize method does. If I didn’t have the line arr = level.constantize and I just tried to do an include? on the level variable above, I’d get an error that include? is not a method for a string. But after the constantize method is run, it finds the array just fine.