[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Strange question: Convert string to array name

kenbo

2/20/2007 12:16:00 AM

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 a string in a variable.
foo = "bobo"

I want to use foo to access an array called bobo. I will have bobo
defined already.

Is there a way to do this?

Take foo and use it to access bobo[2], for example.

I thought about turning foo into a symbol, but that wasn't the way.
And, I think there
should be a way to ask Ruby to return an object via its string name,
but I am
missing the way.

any advice would be appreciated.


kenny

8 Answers

Stephen Duncan Jr

2/20/2007 12:56:00 AM

0

I'm still pretty much a newbie, but my first thought that works is:

eval('bobo')[2]

-Stephen

On 2/19/07, kenbo <kenneth.moorman@gmail.com> 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 a string in a variable.
> foo = "bobo"
>
> I want to use foo to access an array called bobo. I will have bobo
> defined already.
>
> Is there a way to do this?
>
> Take foo and use it to access bobo[2], for example.
>
> I thought about turning foo into a symbol, but that wasn't the way.
> And, I think there
> should be a way to ask Ruby to return an object via its string name,
> but I am
> missing the way.
>
> any advice would be appreciated.
>
>
> kenny
>
>
>


--
Stephen Duncan Jr
www.stephenduncanjr.com

Aaron Patterson

2/20/2007 1:00:00 AM

0

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 a string in a variable.
> foo = "bobo"
>
> I want to use foo to access an array called 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 the
array then 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 Patterson
http://tenderlovem...

kenbo

2/20/2007 1:10:00 AM

0

On Feb 19, 7:59 pm, Aaron Patterson <aaron_patter...@speakeasy.net>
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://tenderlovema... 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 :-)

It's nice to see more similarities between LISP and Ruby.

kenny

SonOfLilit

2/20/2007 1:47:00 AM

0

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/wi...

On 2/20/07, kenbo <kenneth.moorman@gmail.com> wrote:
> On Feb 19, 7:59 pm, Aaron Patterson <aaron_patter...@speakeasy.net>
> 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://tenderlovema... 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 :-)
>
> It's nice to see more similarities between LISP and Ruby.
>
> kenny
>
>
>

Morton Goldberg

2/20/2007 2:11:00 AM

0

On Feb 19, 2007, at 8:10 PM, kenbo wrote:

> 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 :-)
>
> It's nice to see more similarities between LISP and Ruby.

I don't know whether or not you will think this better than using
'eval', but in Ruby this kind of indirect reference problem can often
be handled by introducing a hash. For example,

<code>
a = %w{ one two three four five }
b = %w{ six seven eight nine }
h = {}
h['a'] = a
h['b'] = b
foo = 'a'
h[foo][2] # => "three"
foo = 'b'
h[foo][2] # => "eight"
</code>

Regards, Morton



Gary Wright

2/20/2007 2:15:00 AM

0


On Feb 19, 2007, at 7:20 PM, kenbo wrote:
> I want to use foo to access an array called bobo. I will have bobo
> defined already.

This type of question seems to come up periodically on the list but
I never seem to run into in while coding. I tend to think that in most
(but not all) cases the need to reach for eval indicates some larger
design issue.

In your case, I would ask why you are resorting to using several local
variables that you need to distinguish by name rather than simply using
a hash to organize things?

arrays = {}
array[:bobo] = [1,2,3]
array[:foo] = [4,5,6]

array[:foo][2] # 5

Perhaps if you gave the list the 'bigger' picture they could suggest
a solution that doesn't require 'eval'.

Gary Wright




Robert Dober

2/20/2007 10:10:00 AM

0

On 2/20/07, SonOfLilit <sonoflilit@gmail.com> 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/wi...
>
> On 2/20/07, kenbo <kenneth.moorman@gmail.com> wrote:
> > On Feb 19, 7:59 pm, Aaron Patterson <aaron_patter...@speakeasy.net>
> > 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://tenderlovema... 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

Evan Weaver

2/20/2007 11:24:00 AM

0


It's very unusual to need to dynamically reference a local variable. In
Ruby, local variables usually are highly, er, localized. An instance
variable is much more frequently used in the way you describe, and you
can access it, as Robert says, like so:

>> @foo = "the value"
=> "the value"
>> name = "foo"
=> "foo"
>> instance_variable_get("@#{name}")
=> "the value"

Evan Weaver

--
Posted via http://www.ruby-....