In Ruby, is there are better way of selecting a constant (or avoiding the constant altogether) based

Posted by Vertis on Stack Overflow See other posts from Stack Overflow or by Vertis
Published on 2010-04-16T03:20:52Z Indexed on 2010/04/16 3:23 UTC
Read the original article Hit count: 195

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about architecture