I end up with a string ($1 == “Foo”) which I want to be an instance of Class
(== Foo). How can I convert a String to a Class? Will I have to use
ObjectSpace to find a Class whose Class#name == “Foo”?
At Sat, 21 Dec 2002 18:00:31 +0900, Tim Bates wrote:
I have a class being used in a unit test:
TC_Foo
I match it against a regexp:
/TC_(.*)/.match(self.class.name)
I end up with a string ($1 == “Foo”) which I want to be an instance of Class
(== Foo). How can I convert a String to a Class? Will I have to use
ObjectSpace to find a Class whose Class#name == “Foo”?
I end up with a string ($1 == “Foo”) which I want to be an instance of Class
(== Foo). How can I convert a String to a Class? Will I have to use
ObjectSpace to find a Class whose Class#name == “Foo”?
Tim Bates
Are you ready, Tim? This could hardly be easier. Are you sitting down?
I end up with a string ($1 == “Foo”) which I want to be an instance of Class
(== Foo). How can I convert a String to a Class? Will I have to use
ObjectSpace to find a Class whose Class#name == “Foo”?
I end up with a string ($1 == “Foo”) which I want to be an instance of Class
(== Foo). How can I convert a String to a Class? Will I have to use
ObjectSpace to find a Class whose Class#name == “Foo”?
Although, in the end I used Object.const_get because it makes my intentions a
bit clearer, although the error messages for a nonexistant class are almost
identical. I guess I didn’t think of it because in that sort of situation I
consider eval a bit of a hack.
I end up with a string ($1 == “Foo”) which I want to be an instance of Class
(== Foo). How can I convert a String to a Class? Will I have to use
ObjectSpace to find a Class whose Class#name == “Foo”?
There’s a page on the Wiki with a number of solutions:
I don’t know if the original poster needs it, but The examples there
don’t work with anonymous classes or non-colon delimited class names.
The bit I wrote does.
Although, in the end I used Object.const_get because it makes my intentions a
bit clearer, although the error messages for a nonexistant class are almost
identical. I guess I didn’t think of it because in that sort of situation I
consider eval a bit of a hack.
I agree. I’ve never needed to do this before, and wouldn’t have thought of
Object.const_get. In fact, I only half get that…