views:

32

answers:

1

Not sure the title fully describes the problem/question I'm trying to ask, sorry.

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 (EDIT: The incoming string will be the name of the constant exactly). 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.

+4  A: 
>> ModelType.const_get("MODEL1")

=> "model1"

Module const_get() rdoc

wesgarrison
Thanks. That will work.
Vertis