dynamic object creation

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default dynamic object creation

    If I have something like this:

    s="Array"

    How whould I get something like this:

    aObject=s.new

    In "plain" english I am asking if I have a valid name of a class in a
    string how do I create an instance of the class the string contains?
    Aryeh Friedman Guest

  2. Similar Questions and Discussions

    1. dynamic xml creation
      How can I make XML dynamically inside tha Flash using AS? Basicly i need to make structure like these one: <root> <item></item> ......
    2. Dynamic DataGridRow creation
      Dear all, I am working with a datagrid populated by an XML via a WebService and I would like to populate my dataGrid dynamicaly (as I browse the...
    3. dynamic tag creation
      Looking for a way to dynamically evaluate a tag from a string. eg: <cfset tmp = '<cfset myRslt = "Hello">'> <cfoutput> #tmp# </cfoutput> ...
    4. Dynamic content creation
      I'm rather a newbie to flex, at the stage of considering it it is worth my time to get to know to it (or maybe laszlo...). I have this question:...
    5. Dynamic Sprite Creation
      Hi, I am writing a data driven application that reads a configuration file and populates a director stage with the content of the file. I am not...
  3. #2

    Default Re: dynamic object creation

    On Mon, 30 Jun 2003 16:49:05 +0000, Aryeh Friedman wrote:
    > If I have something like this:
    >
    > s="Array"
    >
    > How whould I get something like this:
    >
    > aObject=s.new
    >
    > In "plain" english I am asking if I have a valid name of a class in a
    > string how do I create an instance of the class the string contains?

    How about this ?

    s="Array"
    aObject=eval(s+".new")


    --
    Simon Strandgaard
    Simon Strandgaard Guest

  4. #3

    Default Re: dynamic object creation

    On Mon, 30 Jun 2003 15:49:05 +0000, Aryeh Friedman wrote:
    > If I have something like this:
    >
    > s="Array"
    >
    > How whould I get something like this:
    >
    > aObject=s.new
    >
    > In "plain" english I am asking if I have a valid name of a class in a
    > string how do I create an instance of the class the string contains?
    s = "Array"
    aObject = eval(s).new

    --
    ste

    stefano Guest

  5. #4

    Default Re: dynamic object creation

    On Tue, Jul 01, 2003 at 01:22:22AM +0900, stefano wrote:
    > On Mon, 30 Jun 2003 15:49:05 +0000, Aryeh Friedman wrote:
    >
    > > If I have something like this:
    > >
    > > s="Array"
    > >
    > > How whould I get something like this:
    > >
    > > aObject=s.new
    > >
    > > In "plain" english I am asking if I have a valid name of a class in a
    > > string how do I create an instance of the class the string contains?
    >
    > s = "Array"
    > aObject = eval(s).new
    ... and just hope nobody passes in s = "`rm -rf /*`" as a class name :-|

    const_get will be much faster too, as it doesn't have to compile a piece of
    code each time.

    Brian Candler Guest

  6. #5

    Default Re: dynamic object creation

    John Platte wrote:
    > On the page you referenced, the entry for const_defined? says it takes a
    > symbol too, but the example code uses a string argument...!? Same for
    > const_set.
    >> I just tested it and to my surprise it worked?!
    >>
    >> [url]http://rubycentral.com/book/ref_c_module.html#Module.const_get[/url]
    >>
    >> says that I need to supply a symbol, why does it accept a string?
    At the top of the section, you'll find "In the descriptions that follow,
    the parameter aSymbol refers to a symbol, which is either a quoted
    string or a Symbol (such as :name)."

    Cheers


    Dave



    Dave Thomas Guest

  7. #6

    Default Re: dynamic object creation

    [url]http://rubycentral.com/book/ref_c_module.htm[/url]

    I spoke too soon. At the top of the page:
    > In the descriptions that follow, the parameter aSymbol refers to a
    > symbol, which is either a quoted string or a Symbol (such as :name )
    Sorry for the spurious post.

    On Monday, Jun 30, 2003, at 15:33 America/Chicago, John Platte wrote:
    > On the page you referenced, the entry for const_defined? says it takes
    > a symbol too, but the example code uses a string argument...!? Same
    > for const_set.
    >
    > On Monday, Jun 30, 2003, at 15:23 America/Chicago, Anders Borch wrote:
    >
    >>> A class name is just a constant, so something like
    >>> a = Module.const_get("Array").new
    >>
    >> uhm... why does this work?
    >>
    >> I just tested it and to my surprise it worked?!
    >>
    >> [url]http://rubycentral.com/book/ref_c_module.html#Module.const_get[/url]
    >>
    >> says that I need to supply a symbol, why does it accept a string?
    >>
    >> is this something that will continue to work in 1.8? (im still using
    >> 1.6.8 on my winbox and 1.6.7 on my linux box)
    --
    John Platte
    Principal Consultant, NIKA Consulting
    [url]http://nikaconsulting.com/[/url]
    (630) 499 9830
    Fax: (630) 566 0655


    John Platte Guest

  8. #7

    Default Re: dynamic object creation

    Brian Candler wrote:
    > On Tue, Jul 01, 2003 at 01:01:25AM +0900, Aryeh Friedman wrote:
    >
    >>If I have something like this:
    >>
    >>s="Array"
    >>
    >>How whould I get something like this:
    >>
    >>aObject=s.new
    >>
    >>In "plain" english I am asking if I have a valid name of a class in a
    >>string how do I create an instance of the class the string contains?
    >
    >
    > A class name is just a constant, so something like
    >
    > a = Module.const_get("Array").new
    uhm... why does this work?

    I just tested it and to my surprise it worked?!

    [url]http://rubycentral.com/book/ref_c_module.html#Module.const_get[/url]

    says that I need to supply a symbol, why does it accept a string?

    is this something that will continue to work in 1.8? (im still using
    1.6.8 on my winbox and 1.6.7 on my linux box)
    >
    > is what you need (Aside: what's the proper way to access 'the top-level
    > Module', or is the above correct?)
    >
    > Cheers,
    >
    > Brian.
    >

    --
    dc -e
    4dd*od3*dddn1-89danrn10-dan3+ann6*dan*2*an13dn1+dn2-dn3+5*ddan2/9+an13nap

    Anders Borch Guest

  9. #8

    Default Re: dynamic object creation

    Dave Thomas wrote:
    > John Platte wrote:
    >
    >> On the page you referenced, the entry for const_defined? says it takes
    >> a symbol too, but the example code uses a string argument...!? Same
    >> for const_set.
    >
    >
    >>> I just tested it and to my surprise it worked?!
    >>>
    >>> [url]http://rubycentral.com/book/ref_c_module.html#Module.const_get[/url]
    >>>
    >>> says that I need to supply a symbol, why does it accept a string?
    >
    >
    > At the top of the section, you'll find "In the descriptions that follow,
    > the parameter aSymbol refers to a symbol, which is either a quoted
    > string or a Symbol (such as :name)."
    >
    thanks alot :) I'll try to *read* from now on ;)

    --
    dc -e
    4dd*od3*dddn1-89danrn10-dan3+ann6*dan*2*an13dn1+dn2-dn3+5*ddan2/9+an13nap

    Anders Borch Guest

  10. #9

    Default Re: dynamic object creation

    On Tue, 01 Jul 2003 05:01:08 +0900, Brian Candler wrote:
    > .. and just hope nobody passes in s = "`rm -rf /*`" as a class name :-|
    obviously, if you don't check what you're passing to eval(), you deserve
    all the bad things that could happen :-)
    > const_get will be much faster too, as it doesn't have to compile a piece of
    > code each time.
    that's true, I didn't think about it.

    --
    ste
    stefano Guest

  11. #10

    Default Re: dynamic object creation

    Brian Candler wrote:
    > On Tue, Jul 01, 2003 at 05:23:35AM +0900, Anders Borch wrote:
    >
    >>>A class name is just a constant, so something like
    >>>
    >>> a = Module.const_get("Array").new
    >>
    >>uhm... why does this work?
    >>
    >>I just tested it and to my surprise it worked?!
    >>
    >>[url]http://rubycentral.com/book/ref_c_module.html#Module.const_get[/url]
    >>
    >>says that I need to supply a symbol, why does it accept a string?
    >
    >
    > Even if it didn't, you can easily convert strings to symbols and vice versa.
    >
    > "Array".intern
    > => :Array
    hadn't seen that one, thanks =)
    > :Array.to_s
    > => "Array"
    >
    > Regards,
    >
    > Brian.
    >

    --
    dc -e
    4dd*od3*dddn1-89danrn10-dan3+ann6*dan*2*an13dn1+dn2-dn3+5*ddan2/9+an13nap

    Anders Borch Guest

  12. #11

    Default Re: dynamic object creation


    "Brian Candler" <B.Candler@pobox.com> schrieb im Newsbeitrag
    news:20030630171414.A19372@linnet.org...
    > On Tue, Jul 01, 2003 at 01:01:25AM +0900, Aryeh Friedman wrote:
    > > If I have something like this:
    > >
    > > s="Array"
    > >
    > > How whould I get something like this:
    > >
    > > aObject=s.new
    > >
    > > In "plain" english I am asking if I have a valid name of a class in a
    > > string how do I create an instance of the class the string contains?
    >
    > A class name is just a constant, so something like
    >
    > a = Module.const_get("Array").new
    >
    > is what you need (Aside: what's the proper way to access 'the top-level
    > Module', or is the above correct?)
    I think this does it

    a = Kernel.const_get("Array").new

    robert

    Robert Klemme Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139