[Harrisburg-ruby-talk] String as class name
n_powell
nathan at nathanpowell.org
Sat May 5 23:49:00 EDT 2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Got it.
inst = Kernel.const_get(klass).new
inst is a new instance of the class from the string.
Had to get the googling just right:
http://infovore.org/tag/ruby/page/2/
On Sat, May 05, 2007 at 11:13:31PM -0400 thus spake n_powell:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hello.
>
> Not sure how many people are sub'd to this list, but I figured this
> is as good a place as any to ask a question.
>
> I am working a project, and in the code I want to slurp a dir of files.
>
> The files each contain ruby classes, named similar to the file name.
> For example, foo.rb, contains the class Foo. With some regexing I
> have taken the file name, and from that extrapolated the class name.
>
> Example:
>
> klass = file.capitalize.gsub(/.rb$/, '')
>
> Now I want to create an instance of the Foo class. I am unsure how
> to do that since klass is a String object.
>
> Any ideas? Or do I need to provide more info?
> - --
> nathan
> nathan_at_nathanpowell_dot_org
>
> The longer I live the more I see that I am never wrong about anything,
> and that all the pains I have so humbly taken to verify my notions have only wasted my time.
> ~ George Bernard Shaw
> - ------------------------------------
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.6 (GNU/Linux)
>
> iD8DBQFGPUfbv3H2lx1re5YRAhC2AJ9EETh7Cou7k7x0WvVNiO6pJPADGQCfV5aj
> /vfV5YQ/3OZAIa4G+8wrSGg=
> =MlOO
> -----END PGP SIGNATURE-----
> _______________________________________________
> Harrisburg-ruby-talk mailing list
> Harrisburg-ruby-talk at rubyforge.org
> http://rubyforge.org/mailman/listinfo/harrisburg-ruby-talk
- --
nathan
nathan_at_nathanpowell_dot_org
If you optimize everything, you will always be unhappy.
~ Donald Knuth
- ------------------------------------
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
iD8DBQFGPVAsv3H2lx1re5YRAm6CAJ4g5r76GvnbNXwXeuDm08ctHhl8XwCfZX5R
qHBwJ02k6S8MMrSGU0lBOHU=
=J9bo
-----END PGP SIGNATURE-----
More information about the Harrisburg-ruby-talk
mailing list