From: Assaph Mehr Date: 2005-04-12T07:19:38+09:00 Subject: Re: Factory design > In any case, I have an identifier on my log file that looks like "2005-01-04 > 23:34:12 begin_phase". If I see this, I want to create a "Phase" object. If > it says "2005-01-04 23:52:16 begin_interruption", then I want to create an > interruption instead. All class objects are kept as constants of Object. In fact when you write a class name in your script Ruby treats it like any other constant (starts with a capital letters) - it's just in Object name space so every object can access it. Thus you can get them like any other constant in your script: c:\>irb irb(main):001:0> class Phase; end => nil irb(main):002:0> s = "2005-01-04 23:34:12 begin_phase" => "2005-01-04 23:34:12 begin_phase" irb(main):003:0> klass_name = s.match(/begin_(\w+)/).captures[0] rescue nil => "phase" irb(main):006:0> klass = Object.const_get(klass_name.capitalize) if klass_name => Phase irb(main):007:0> o = klass.new => # As a further note, when you come to use the klass variable at runtime remember that case statements use the #=== method which is slightly different for Class objects - it tests if an object is an instance of the the class: case klass when Phase puts 'new phase' when Object puts 'something else' end Will print: 'something else'. You need something like: case klass.name when 'Phase' puts 'new phase' when 'Object' puts 'something else' end Which will print: 'new phase'. HTH, Assaph