In Ruby, is there are better way of selecting a constant (or avoiding the constant altogether) based
- by Vertis
Not sure the title fully describes the problem/question I'm trying to ask, sorry. I'm
One of my fellow developers has created classes as such:
class Widget
attr_accessor :model_type
...
end
and:
class ModelType
MODEL1 = "model1"
MODEL2 = "model2"
MODEL3 = "model3"
end
Now he wants me to convert a retrieved string "MODEL1" to the constant. So that when he is referencing that model elsewhere he can use ModelType::MODEL1. Obviously I've got to convert from the string I'm being given with something like the following:
case model_type
when 'MODEL1'
@model_type = ModelType::MODEL1
...
end
I feel like this is clunky, so I'd like to know if there is a better DRYer way of providing this kind of functionality.