From: Robert Dober Date: 2007-02-20T19:10:00+09:00 Subject: Re: Strange question: Convert string to array name On 2/20/07, SonOfLilit wrote: > There should be, also I don't remember it. > > For all kinds of variables except for local I know the ways, but local > elludes me :) > > Aur Saraf > > PLUG: http://rubymentor.rubyforge.org/wiki/wiki.pl > > On 2/20/07, kenbo wrote: > > On Feb 19, 7:59 pm, Aaron Patterson > > wrote: > > > On Tue, Feb 20, 2007 at 09:20:13AM +0900, kenbo wrote: > > > > I am coming to Ruby from LISP where I was more competent when it came > > > > to symbol > > > > manipulation :-) I have spent some time trying to solve this problem > > > > and figure it's my > > > > general lack of deep knowledge ala Ruby that keeps me from seeing an > > > > "easy" solution. > > > > > > > With that said, I apologize if this has been answered and I missed > > > > it. > > > > > > > I have astringin a variable. > > > > foo = "bobo" > > > > > > > I want to use foo to access anarraycalled bobo. I will have bobo > > > > defined already. > > > > > > > Is there a way to do this? > > > > > > eval() might be what you're looking for. You can use eval to return thearraythen index in to it. Here's an example from: > > > > > > irb(main):001:0> awesome_array = %w{ one two three four five } > > > => ["one", "two", "three", "four", "five"] > > > irb(main):002:0> foo = 'awesome_array' > > > => "awesome_array" > > > irb(main):003:0> puts eval(foo)[1] > > > two > > > => nil > > > irb(main):004:0> > > > > > > Hope that helps! > > > > > > -- > > > Aaron Pattersonhttp://tenderlovemaking.com/- Hide quoted text - > > > > > > - Show quoted text - > > > > Thanks for the folks that mentioned eval---I thought of that after the > > posting but > > assumed that, as in LISP, there might be a better way to accomplish > > it :-) Are you thinking about Ma****, do not mention it anymore on this list ;) Well it is not really the way of Ruby. > > > > It's nice to see more similarities between LISP and Ruby. > > > > kenny > > > > > > > > There is if you can get away from the local variable as somebody mentioned without sharing his wisdom with us ;) If we want to access an array @bobo there is instance_variable_get( "@'" << foo.to_s ).first and all kind of variations. Cheers Robert -- We have not succeeded in answering all of our questions. In fact, in some ways, we are more confused than ever. But we feel we are confused on a higher level and about more important things. -Anonymous