tags:

views:

57

answers:

1
class_name = 'Question'
instance = which_method(class_name)

Which method I should use?

+7  A: 
class_name = 'Question'
klass = Kernel.const_get class_name # klass holds the class Question, which is an object
instance = klass.new
Chubas
short and sweet!
Bragboy
@Chubas, thank you!
Freewind
You don't need the `to_sym`. `const_get` also accepts a string.
sepp2k
Totally right. For some reason I remembered it only accepted a sym. RDoc is misleading in that aspect (Module#const_get). Thanks for the clarification
Chubas