9

I have an array ["agreement", "user", "client"]. Is there any way to convert its items into objects @agreement, @user, @client?

4
  • Similar question addressed on this post - stackoverflow.com/questions/1287648/… Commented Apr 2, 2015 at 10:04
  • Do you have corresponding classes of those inside the Array ? Commented Apr 2, 2015 at 10:04
  • Could you clarify your subject more ? Commented Apr 2, 2015 at 10:08
  • 2
    "convert items into objects" - the items already are objects, more specific: instances of class String. How are these strings related to instance variables with the same name and what are you trying to do? Commented Apr 2, 2015 at 10:09

1 Answer 1

12
["agreement", "user", "client"]
.map{|k| instance_variable_get("@#{k}")}
Sign up to request clarification or add additional context in comments.

1 Comment

Note that for some contexts, we may want instance_variable_set instead of instance_variable_get.

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.