Dynamic object construction

Discussion in 'Ruby' started by John Lam, Apr 19, 2005.

  1. John Lam

    John Lam Guest

    ------_=_NextPart_001_01C5448D.792B9DCA
    Content-Type: text/plain;
    charset="US-ASCII"
    Content-Transfer-Encoding: quoted-printable

    I'm trying to create a new object where the class of the object is
    parameterized. My current solution is to use eval():

    =20

    def create(class)

    @obj =3D eval("#{class}.new()")

    end

    =20

    Is there a better way of doing this to avoid the hit of eval()?

    =20

    Thanks,

    -John

    http://www.iunknown.com

    =20

    =20


    ------_=_NextPart_001_01C5448D.792B9DCA--
     
    John Lam, Apr 19, 2005
    #1
    1. Advertising

  2. John Lam

    Bill Atkins Guest

    ------=_Part_346_25718566.1113881046339
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    If class is a a class name (a String):

    @obj =3D Object.const_get(klass).new

    Using "class" as a parameter name won't work - the standard is to use=20
    "klass" instead.

    Bill

    On 4/18/05, John Lam <> wrote:
    >=20
    > I'm trying to create a new object where the class of the object is
    > parameterized. My current solution is to use eval():
    >=20
    > def create(class)
    >=20
    > @obj =3D eval("#{class}.new()")
    >=20
    > end
    >=20
    > Is there a better way of doing this to avoid the hit of eval()?
    >=20
    > Thanks,
    >=20
    > -John
    >=20
    > http://www.iunknown.com
    >=20
    >=20



    --=20
    Bill Atkins

    ------=_Part_346_25718566.1113881046339--
     
    Bill Atkins, Apr 19, 2005
    #2
    1. Advertising

  3. > @obj = Object.const_get(klass).new

    does this work if klass == My::Namespace::Klass ?

    -g.
     
    George Moschovitis, Apr 20, 2005
    #3
  4. John Lam

    Trans Guest

    George Moschovitis wrote:
    > > @obj = Object.const_get(klass).new

    >
    > does this work if klass == My::Namespace::Klass ?


    Assuming you mean the string value

    klass = "My::Namespace::Klass"

    The answer is No. One has to do:

    klass = "Klass"
    @obj = My::Namespace.const_get(klass).new

    The namespace issue is problematic. One alternative is in Ruby Facets:

    require 'facet/object/constant'

    klass == "My::Namespace::Klass"
    @obj = constant(klass).new

    T.
     
    Trans, Apr 20, 2005
    #4
  5. > The answer is No. One has to do:
    > klass = "Klass"
    > @obj = My::Namespace.const_get(klass).new


    I was expecting the negative answer ;-)

    > One alternative is in Ruby Facets


    Interesting, I 'll see how that works...

    -g.
     
    George Moschovitis, Apr 21, 2005
    #5
  6. On 4/20/05, Trans <> wrote:
    > Assuming you mean the string value
    > klass = "My::Namespace::Klass"
    > The answer is No. One has to do...


    Or the ever popular

    klass.split('::').inject(Object) {|parent, name| parent.const_get(name)}
     
    Nicholas Seckar, Apr 21, 2005
    #6
    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. Anthony
    Replies:
    1
    Views:
    308
    Victor Bazarov
    Sep 23, 2003
  2. Sakcee
    Replies:
    5
    Views:
    458
    bruno at modulix
    Mar 30, 2006
  3. Ook
    Replies:
    10
    Views:
    561
  4. pete m

    dynamic construction of va_list

    pete m, Apr 28, 2007, in forum: C Programming
    Replies:
    1
    Views:
    714
    Chris Torek
    Jun 4, 2007
  5. cantide5ga
    Replies:
    7
    Views:
    391
    cantide5ga
    Oct 23, 2007
Loading...

Share This Page