creating objects using strings 
Author Message
 creating objects using strings

hi

I'm new to ruby and this list, so please forgive me if this is a silly
question but:

Is it possible to create an object of a specific type when all I have is the
name of the class in a string?

I'm wanting to serialize objects to XML, but I need to have the type
information stored in the XML and create objects of a type based on that
information. I have looked at the clxmlserial library but it needs to know
the type when re-creating objects. I have tried making a class
XmlSerializableObject with _dump and _load methods for use with Marshal, but
the type information is still stored in a non-xml format at the start of the
file. Do I need to write a module in c to do this?

Any help would be greatly appreciated,
--
George Mochrie
aka Drasil
ICQ# 84423409



Mon, 01 Aug 2005 00:15:02 GMT  
 creating objects using strings

G> Is it possible to create an object of a specific type when all I have is the
G> name of the class in a string?

 a class is stored in a constant, if you have its name you can retrieve the
 associated constant with #const_get

 For example

pigeon% ruby -e 'klass = "Array"; p Object.const_get(klass).new'
[]
pigeon%

Guy Decoux



Mon, 01 Aug 2005 00:22:16 GMT  
 creating objects using strings

Quote:

> Is it possible to create an object of a specific type when all I have is the
> name of the class in a string?

There's a page on the Wiki on this:

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

Cheers

Dave



Mon, 01 Aug 2005 01:47:13 GMT  
 creating objects using strings


Quote:

> >Is it possible to create an object of a specific type when all I have is
> >the name of the class in a string?

> There's a page on the Wiki on this:

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

I know we've been through this before, but I thought I'd repeat it
anyway.  I have a slightly more robust version, called String#to_class,
which handles periods, "relative" class paths, and anonymous namespaces.

Check it out at http://www.pablotron.org/download/string_to_class.rb.

PS I wasn't quite sure where to put this on the Wiki page.  Before the
simple example?  After it?  Not at all?  Dave?

Quote:
> Cheers

> Dave

--

http://www.pablotron.org/               OpenPGP Key ID: 0x82C29562

  application_pgp-signature_part
< 1K Download


Mon, 01 Aug 2005 06:24:10 GMT  
 creating objects using strings

Quote:

> > http://www.rubygarden.org/ruby?FindClassesByName
> ...
> Check it out at http://www.pablotron.org/download/string_to_class.rb.

> PS I wasn't quite sure where to put this on the Wiki page.  
> Before the simple example?  After it?

I prefer after it.
I'm a wiki lurker :-)

kind regards -botp



Mon, 01 Aug 2005 15:51:15 GMT  
 creating objects using strings

Quote:

> Check it out at http://www.pablotron.org/download/string_to_class.rb.
> PS I wasn't quite sure where to put this on the Wiki page.  Before the
> simple example?  After it?  Not at all?  Dave?

If it's a big chunk and you don't want to interfere with what's
already there, then put it on a second page and add a link to the
"main" page.

Since this is like a "refactoring" of the wiki page, then a subpage
would probably be appropriate, like

   FindClassByName/ImplementationExample

This can be referenced as "/ImplementationExample" in the
FindClassByName page.

The wiki will be richer for your contribution :)

Gavin



Mon, 01 Aug 2005 17:01:31 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. function-objects and code-objects ( was: re-creating objects and security )

2. Creating a vector of strings using read-char...???

3. Object Rexx using OLE to create message in Outlook

4. Using REXX to create new objects in OS/2 with Desktop Observatory

5. Creating -o32 objects on SGI using Fortran 90

6. Q: Creating an object member function using exec

7. Need an example to create lists in Tcl 8.0 using Object

8. Creating lists in Tcl 8.0 using Object

9. Creating a control at runtime - using Create()

10. How to replace or create a file using the open/create/replace.vi

11. string object methods vs string module functions

12. Assigning to string object in Object Rexx

 

 
Powered by phpBB® Forum Software