Class.forName ?

Discussion in 'Ruby' started by baalbek, May 16, 2005.

  1. baalbek

    baalbek Guest

    In Java there is a construct like this:

    Class clazz = Class.forName("Person");

    Person person = (Person)clazz.newInstance();

    Is there something similar available for Ruby, that is, can I create a
    class on the fly only having this class' name as a string?

    In other words, something like this (for Ruby):

    person = Class.new("Person")

    This does not work, but is there something that would work like this?

    Thanks in advance!

    Baalbek
    baalbek, May 16, 2005
    #1
    1. Advertising

  2. baalbek

    David Naseby Guest

    On 5/16/05, baalbek <> wrote:
    > In Java there is a construct like this:
    >=20
    > Class clazz =3D Class.forName("Person");
    >=20


    irb(main):001:0> class A; def initialize; puts 'in A'; end; end
    =3D> nil
    irb(main):002:0> a =3D eval("A.new")
    in A
    =3D> #<A:0x2ad3400>
    irb(main):003:0> a =3D Module.const_get("A").new
    in A
    =3D> #<A:0x2ad02c8>

    Lots of other ways, I'm sure.


    --=20
    David Naseby
    http://homepages.ihug.com.au/~naseby/
    David Naseby, May 16, 2005
    #2
    1. Advertising

  3. baalbek

    Park Ji-In Guest

    2005-05-16 (월), 09:55 +0900, baalbek 쓰시길:
    > In other words, something like this (for Ruby):
    >
    > person = Class.new("Person")


    how about

    irb(main):006:0> Class.const_get('Array').new
    => []

    or

    irb(main):007:0> Class.const_get:)Array).new
    => []
    Park Ji-In, May 16, 2005
    #3
  4. baalbek

    Mark Hubbart Guest

    On 5/15/05, baalbek <> wrote:
    > In Java there is a construct like this:
    >=20
    > Class clazz =3D Class.forName("Person");
    >=20
    > Person person =3D (Person)clazz.newInstance();
    >=20
    > Is there something similar available for Ruby, that is, can I create a
    > class on the fly only having this class' name as a string?
    >=20
    > In other words, something like this (for Ruby):
    >=20
    > person =3D Class.new("Person")
    >=20
    > This does not work, but is there something that would work like this?


    Perhaps you want:

    Person =3D Class.new
    or
    Object.const_set "Person", Class.new

    Both will create a new class named "Person". The second method will
    let you use a name determined at runtime. If I understand your Java
    code above, the Ruby translation would be something like this:

    klass =3D Object.const_set("Person", Class.new)
    =20
    person =3D klass.new

    HTH,
    Mark
    Mark Hubbart, May 16, 2005
    #4
  5. baalbek

    baalbek Guest

    Park Ji-In wrote:
    > irb(main):006:0> Class.const_get('Array').new


    Ahh, exactly what I needed! Thanks!

    Baalbek
    baalbek, May 16, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. iksrazal

    Re: Class.forname() vs clone()

    iksrazal, Jun 27, 2003, in forum: Java
    Replies:
    0
    Views:
    1,601
    iksrazal
    Jun 27, 2003
  2. Ahmed Moustafa

    performance of Class.forname()?

    Ahmed Moustafa, Jul 20, 2003, in forum: Java
    Replies:
    14
    Views:
    5,900
    Chris Smith
    Jul 29, 2003
  3. H.MuthuKumaraRajan

    .class and Class.forName

    H.MuthuKumaraRajan, Nov 6, 2003, in forum: Java
    Replies:
    11
    Views:
    1,725
    H.MuthuKumaraRajan
    Nov 7, 2003
  4. cyril
    Replies:
    2
    Views:
    3,853
    cyril
    Aug 25, 2004
  5. Madni
    Replies:
    1
    Views:
    5,878
Loading...

Share This Page