I have a set of classes with static methods. Example member of class set:
class George
def self.ugh()
printf( "Hello world\n" )
end # self.ugh()
end # class george
I need to do the following, but I don’t have what I need.
p George.object_id
The code I’m working with passes the class name (aka var). I’ve found a
solution that seems ugly, but it works.
var = "George" # What I have
cmd = "#{var}.object_id"
p eval( cmd ) # right object_id
I would think there’s a better way. Seems like ruby’s missing a
s_to_class() method.
The only other ideas I’ve found, that don’t work.
klass = class << var; self; end
p klass.object_id # wrong object_id
klass = var.singleton_class()
p klass.object_id # wrong object_id
Anyone know a better way to get a class object (aka receiver) from a
string/symbol?
ActiveSupport provides a
constantizemethod, if you’ve got that available. Otherwise, useModule#const_get. See this question for more details.