intargc intargc - 11 months ago 54
Ruby Question

Ruby symbol to class

Is there a way in Ruby to take a symbol or string and turn it into a class of the same name?

For instance, if I have a class such as

class Bob
def talk
puts "Hi, I'm bob"

And a method I have somewhere else in the code is passed a symbol :bob, can I in some way turn that into the class Bob? Maybe something like

b =

Or is there a way to do something similar to this?

Answer Source

There are many ways to do this. Your lack of context makes it impossible to elect a "best" way. Here's a few ayways.