Meta programming basics

K

Kevin Olbrich

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
 
B

Brian Schröder

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/
 
G

Gavin Kistner

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/
 
B

Brian Takita

------=_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"

=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--
 
B

Brian Takita

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

Oops, sorry Gavin.

=20
You could also use a Hash.
=20
values =3D Hash.new
=20
a =3D "Foo"
values[a] =3D "Bar"
=20
=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--
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top