From: Ned Konz Date: 2001-10-20T01:39:20+09:00 Subject: [ruby-talk:22760] Re: Creating variables with dynamic names ? On Friday 19 October 2001 09:25 am, Andrew Cowan wrote: > I am somewhat new to Ruby, coming from about 10 years of Perl programming, > 6 professionally. > > In perl, it is easy to create new variables with variable names that are > defined in another variable. For example: > > $var_name = "myVar"; > ${"$var_name"} = 10; > > would define a new variable $myVar the value 10. The usefulness of this is quite limited, unless you're writing an interactive environment. Many people just say use strict; at the top of their Perl programs and get away with it. And the symbolic references like the above will be flagged with warnings. > Is there a way to do this in Ruby? I suspect there must be but haven't been > able to figure this out yet. There's eval, instance_eval, etc. $ irb irb(main):001:0> a = "abc" "abc" irb(main):002:0> instance_eval a + "=3" 3 irb(main):003:0> abc 3 -- Ned Konz currently: Stanwood, WA email: ned@bike-nomad.com homepage: http://bike-nomad.com