1

Say I have an array holding some variables such as array = ["one", "two", "three"]. How would I assign array[0] to be a variable? I know this is probably the wrong way of implementing this, but I'm trying something like this:

var = 10   
instance_variable_set("@#{array[0]}", var)

I want to be able to call puts @"#{array[0]}" and it give me 10 as a result.

Edit: I am trying to set the array index (in this case "one") to be a new variable so that I can later call it in other methods. This way set! works for changing the variable later when the array not longer holds the same values. For example, if after setting "one" as a new variable whose value is 10, if i get an array containing ["three", "two", "one"] I want to be able to call puts array[2] and it give me 10.

7
  • why not using #instance_variable_get ? Commented Aug 11, 2014 at 19:49
  • Can you show me an example of using #instance_variable_get to get the above results? Commented Aug 11, 2014 at 20:02
  • 1
    puts instance_variable_get("@#{array[0]}") Commented Aug 11, 2014 at 20:45
  • Can you clarify what you are trying to do? You say you "have an array holding some variables", but that's impossible. Arrays can only hold objects and variables aren't objects, therefore you cannot possibly "have an array holding some variables". What you have shown is an array holding some strings, not variables. Commented Aug 11, 2014 at 21:17
  • I want to CREATE a variable, as in if the index of the array holds a string "one" I want to create a new variable named "one" that I can call later. Already answered by Steve below. Commented Aug 11, 2014 at 22:09

3 Answers 3

3

Just set it like an array in most languages:

array[0] = var
Sign up to request clarification or add additional context in comments.

1 Comment

This just stores the value into the array. I want the array element to be set to a NEW variable I can later call, that hold the value from var.
0
eval "@#{array[0]} = var"

p @one
=> 10

or

eval "p @#{array[0]}"
=> 10

as A Fader Darkly points out, eval is potentially dangerous, particularly if the value comes from user input. What if the user enters, as a string, "`rm -rf /`" ? You've now wiped out everything in your root folder.

7 Comments

I never actually know the name of the variable, so I cannot call it in my code. It would be in an array, so if "one" was at index 0, I'd need to call something like puts "@#{array[0]}"
Yeah you could do that. I'm not clear why you want to but it works.
Except it would be eval "puts @#{array[0]}"
Careful with eval. Never use it if the string may be coming from an untrusted source, as in a worst case it gives that source complete control over your computer. (Ruby can run shell commands...)
This is the most horrendous useless use of eval I have ever seen. What's wrong with instance_variable_set/instance_variable_get?
|
0

If you want to set instance variable you should probably call @var = array[0]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.