Create named objects

Discussion in 'Ruby' started by Roland Schmitt, Mar 1, 2006.

  1. Hello,

    want to do the following:

    name = "input"
    clazz = "String"
    value = "test"

    <<<some magic here>>>

    puts(input) -> "test"
    input.class() -> String


    Thanks in advance,

    Roland
    Roland Schmitt, Mar 1, 2006
    #1
    1. Advertising

  2. Roland Schmitt

    Guest

    Roland Schmitt wrote:
    > Hello,
    >
    > want to do the following:
    >
    > name = "input"
    > clazz = "String"
    > value = "test"
    >
    > <<<some magic here>>>
    >
    > puts(input) -> "test"
    > input.class() -> String
    >
    >
    > Thanks in advance,
    >
    > Roland


    a pretty straight forward way is to just use the eval function on a
    string

    # in your insert magic here:
    evs = "#{name} = #{clazz}.new( #{value} )"
    eval( evs )
    , Mar 1, 2006
    #2
    1. Advertising

  3. Roland Schmitt

    Guest

    Hi --

    On Thu, 2 Mar 2006, wrote:

    >
    > Roland Schmitt wrote:
    >> Hello,
    >>
    >> want to do the following:
    >>
    >> name = "input"
    >> clazz = "String"
    >> value = "test"
    >>
    >> <<<some magic here>>>
    >>
    >> puts(input) -> "test"
    >> input.class() -> String
    >>
    >>
    >> Thanks in advance,
    >>
    >> Roland

    >
    > a pretty straight forward way is to just use the eval function on a
    > string
    >
    > # in your insert magic here:
    > evs = "#{name} = #{clazz}.new( #{value} )"
    > eval( evs )


    That will create a new inner scope, so if there's no variable named
    input already, there won't be one when the eval exits. (Which is
    good, because otherwise we'd probably be seeing a lot of this kind of
    thing :)

    The best advice, though it's not exactly an answer to the question,
    is: use a hash.

    name = "input"
    value = "test"
    value_hash[name] = test

    rather than the "soft reference"-style (to borrow a Perl phrase) way
    of creating locals.


    David

    --
    David A. Black ()
    Ruby Power and Light (http://www.rubypowerandlight.com)

    "Ruby for Rails" chapters now available
    from Manning Early Access Program! http://www.manning.com/books/black
    , Mar 1, 2006
    #3
  4. --Apple-Mail-13--275638156
    Content-Transfer-Encoding: 7bit
    Content-Type: text/plain;
    charset=US-ASCII;
    delsp=yes;
    format=flowed


    On Mar 1, 2006, at 11:33 AM, wrote:

    >
    > Roland Schmitt wrote:
    >> Hello,
    >>
    >> want to do the following:
    >>
    >> name = "input"
    >> clazz = "String"
    >> value = "test"
    >>
    >> <<<some magic here>>>
    >>
    >> puts(input) -> "test"
    >> input.class() -> String
    >>
    >>
    >> Thanks in advance,
    >>
    >> Roland

    >
    > a pretty straight forward way is to just use the eval function on a
    > string
    >
    > # in your insert magic here:
    > evs = "#{name} = #{clazz}.new( #{value} )"
    > eval( evs )


    alternatively:

    name = "input"
    clazz = "String"
    value = "test"

    (class << self; self; end).class_eval { define_method(name)
    { const_get(clazz).new(value) } }

    puts(input)
    puts input.class


    There are several disadvantages to this approach.
    1) input is a method not really a variable and therefore its scope
    is gonna be all sorts of wrong
    2) Since it is a method, assigning to it is not necessarily going to
    work the way you want

    Advantages ares that yo don't have to worry about the having seen an
    assignment to it business and you don't have to invoke the parser

    Yet another method would be
    instance_variable_set("@#{name}", Object.const_get(clazz).new(value))

    Unfortunately now you have to refer to it as @input. It also has
    some scoping issues.
    --Apple-Mail-13--275638156--
    Logan Capaldo, Mar 1, 2006
    #4
    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. John Blair
    Replies:
    1
    Views:
    291
    John Blair
    Dec 22, 2004
  2. 7stud
    Replies:
    11
    Views:
    661
    Dennis Lee Bieber
    Mar 20, 2007
  3. waltbrad
    Replies:
    3
    Views:
    271
    waltbrad
    Mar 17, 2008
  4. mike
    Replies:
    1
    Views:
    308
    Mark Space
    Mar 3, 2009
  5. Paul Blay
    Replies:
    2
    Views:
    76
    Paul Blay
    Dec 7, 2003
Loading...

Share This Page