String#to_class?

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”?

Tim Bates

···


tim@bates.id.au

Hi,

···

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”?

eval($1)

or

Object.const_get($1)


Nobu Nakada

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?

klass = eval $1

:slight_smile:

Cheers,
Gavin

···

From: “Tim Bates” tim@bates.id.au

There’s a page on the Wiki with a number of solutions:

http://www.rubygarden.org/ruby?FindClassesByName

Cheers

Dave

···

On Sat, 2002-12-21 at 03:00, 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 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”?

Hey,

I wrote a String#to_class implementation using const_get for my friend a
few months back. Check out
http://www.pablotron.org/download/string_to_class.rb (there’s test code
and examples included in the file).

···
  • Tim Bates (tim@bates.id.au) wrote:

Tim Bates

tim@bates.id.au


Paul Duncan pabs@pablotron.org pabs in #gah (OPN IRC)
http://www.pablotron.org/ OpenPGP Key ID: 0x82C29562

  • Tim smacks head with palm.

D’oh!

(Respond same to Gavin’s email.)

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.

Tim Bates

···

On Sat, 21 Dec 2002 07:36 pm, nobu.nokada@softhome.net wrote:

eval($1)


tim@bates.id.au

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”?

There’s a page on the Wiki with a number of solutions:

http://www.rubygarden.org/ruby?FindClassesByName

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.

···

On Sat, 2002-12-21 at 03:00, Tim Bates wrote:

Cheers

Dave


Paul Duncan pabs@pablotron.org pabs in #gah (OPN IRC)
http://www.pablotron.org/ OpenPGP Key ID: 0x82C29562

eval($1)

  • Tim smacks head with palm.

D’oh!

(Respond same to Gavin’s email.)

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…

Tim Bates

tim@bates.id.au

Gavin

···

From: “Tim Bates” tim@bates.id.au

On Sat, 21 Dec 2002 07:36 pm, nobu.nokada@softhome.net wrote: