I’m new to Ruby but have got several years of experience with PHP. I
work on a project with several classes witch analyse text blocks. The
blocks begin with headers like “Status” or “Update” but that’s not my
problem.
And with this, you can do the var = Kernel.const_get(“ClassName”) and
you have a reference to that class for the scope of your operation.
imho const_get is just to fragile for most uses, i prefer something
like this:
cfp:~ > cat a.rb
class Class
def self.for string
value =
Thread.new do
$SAFE = 4
eval string.to_s, TOPLEVEL_BINDING.dup
end.value
raise ArgumentError unless value.is_a? Class
value
end
end
p Class.for(‘File::Stat’)
p Class.for(‘Foo::Bar’)
cfp:~ > ruby a.rb
File::Stat
a.rb:6:in eval': (eval):1: uninitialized constant Foo (NameError) from a.rb:4:in value’
from a.rb:4:in `for’
from a.rb:14