Meta programming basics

Discussion in 'Ruby' started by Kevin Olbrich, Aug 22, 2005.

  1. I'm sure I missed this somewhere in the Pickaxe, but what I am looking for
    is a way to do this...

    A = "Foo"

    Assign a value to the variable named in A so at the end of the day it looks
    like..

    Foo = "Bar"

    Thanks,
    _Kevin
    Kevin Olbrich, Aug 22, 2005
    #1
    1. Advertising

  2. On 22/08/05, Kevin Olbrich <> wrote:
    > I'm sure I missed this somewhere in the Pickaxe, but what I am looking fo=

    r
    > is a way to do this...
    >=20
    > A =3D "Foo"
    >=20
    > Assign a value to the variable named in A so at the end of the day it loo=

    ks
    > like..
    >=20
    > Foo =3D "Bar"
    >=20
    > Thanks,
    > _Kevin
    >=20
    >=20
    >=20
    >=20
    >=20


    You have to beware that eval'ed local variables are only accessible by
    eval. (And don't let irb fool you, it evals everything, so what works
    there need not work in a program.)

    a =3D "t"
    eval("#{a} =3D 42")
    eval("t") #=3D> 42

    regards,

    Brian

    --=20
    http://ruby.brian-schroeder.de/

    Stringed instrument chords: http://chordlist.brian-schroeder.de/
    Brian Schröder, Aug 22, 2005
    #2
    1. Advertising

  3. On Aug 22, 2005, at 7:15 AM, Kevin Olbrich wrote:
    > I'm sure I missed this somewhere in the Pickaxe, but what I am
    > looking for
    > is a way to do this...
    >
    > A = "Foo"
    >
    > Assign a value to the variable named in A so at the end of the day
    > it looks
    > like..
    >
    > Foo = "Bar"



    I'm not aware of a way to do this with local variables, other than
    eval. (But then, I just figured out Struct, so what do I know? :p)
    However, with a Hash or OpenStruct, this is easy:

    dynamic_items = {}
    prop_name = "Foo"
    dynamic_items[ prop_name ] = "Bar"
    p dynamic_items #=> {"Foo"=>"Bar"}
    puts dynamic_items[ "Foo" ] #=> Bar


    require 'ostruct'
    dynamic2 = OpenStruct.new
    dynamic2.custom_name = 'bar'
    dynamic2.send( prop_name + '=', 'Bar' )
    puts dynamic2.Foo #=> Bar


    See also SuperStruct, for a mix of both [] and dot notation (and more):
    http://sstruct.rubyforge.org/
    Gavin Kistner, Aug 22, 2005
    #3
  4. Kevin Olbrich

    Guest

    Gavin Kistner wrote:

    >
    > See also SuperStruct, for a mix of both [] and dot notation (and more):
    > http://sstruct.rubyforge.org/


    I haven't touched SuperStruct in a while... it definitely has some
    bugs in it, so if you or anyone run across one, let me know.


    Hal
    , Aug 22, 2005
    #4
  5. Kevin Olbrich

    Brian Takita Guest

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

    You could also use a Hash.

    values =3D Hash.new

    a =3D "Foo"
    values[a] =3D "Bar"

    On 8/22/05, <> wrote:
    >=20
    > Gavin Kistner wrote:
    >=20
    > >
    > > See also SuperStruct, for a mix of both [] and dot notation (and more):
    > > http://sstruct.rubyforge.org/

    >=20
    > I haven't touched SuperStruct in a while... it definitely has some
    > bugs in it, so if you or anyone run across one, let me know.
    >=20
    >=20
    > Hal
    >=20
    >=20
    >


    ------=_Part_913_30639.1124775023466--
    Brian Takita, Aug 23, 2005
    #5
  6. Kevin Olbrich

    Brian Takita Guest

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

    Oops, sorry Gavin.

    On 8/22/05, Brian Takita <> wrote:
    >=20
    > You could also use a Hash.
    >=20
    > values =3D Hash.new
    >=20
    > a =3D "Foo"
    > values[a] =3D "Bar"
    >=20
    > On 8/22/05, < > wrote:
    > >=20
    > > Gavin Kistner wrote:
    > >=20
    > > >
    > > > See also SuperStruct, for a mix of both [] and dot notation (and=20

    > > more):=20
    > > > http://sstruct.rubyforge.org/

    > >=20
    > > I haven't touched SuperStruct in a while... it definitely has some
    > > bugs in it, so if you or anyone run across one, let me know.
    > >=20
    > >=20
    > > Hal
    > >=20
    > >=20
    > >=20

    >


    ------=_Part_937_14787955.1124775173003--
    Brian Takita, Aug 23, 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. Nym Pseudo

    META NAME and META HTTP-EQUIV

    Nym Pseudo, Sep 26, 2003, in forum: HTML
    Replies:
    1
    Views:
    528
    =?iso-8859-1?Q?brucie?=
    Sep 26, 2003
  2. manish
    Replies:
    1
    Views:
    309
    Christopher Benson-Manica
    Apr 2, 2004
  3. Duane Johnson

    Meta methods to govern meta data?

    Duane Johnson, Oct 25, 2005, in forum: Ruby
    Replies:
    6
    Views:
    220
    Adam Sanderson
    Oct 28, 2005
  4. Erik Veenstra

    Meta-Meta-Programming

    Erik Veenstra, Feb 7, 2006, in forum: Ruby
    Replies:
    29
    Views:
    349
    Erik Veenstra
    Feb 8, 2006
  5. Erik Veenstra

    Meta-Meta-Programming, revisited

    Erik Veenstra, Jul 21, 2006, in forum: Ruby
    Replies:
    21
    Views:
    420
    Erik Veenstra
    Jul 25, 2006
Loading...

Share This Page