From: Andrew Cowan Date: 2001-10-20T04:46:16+09:00 Subject: [ruby-talk:22793] Re: Creating variables with dynamic names ? Ned Konz wrote: > 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 > I dont think the usefulness is limited at all, obviously the code isn't meant to be used in other people's code where use strict is being used... However, I appreciate the example of instance_eval's usage! ______________________________________________________________________ Posted Via Uncensored-News.Com - Still Only $9.95 - http://www.uncensored-news.com With NINE Servers In California And Texas - The Worlds Uncensored News Source