Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to instance variables

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

like image 525
gmrash Avatar asked Apr 02 '15 10:04

gmrash


People also ask

How do you convert a string to a variable?

String Into Variable Name in Python Using the vars() Function. Instead of using the locals() and the globals() function to convert a string to a variable name in python, we can also use the vars() function. The vars() function, when executed in the global scope, behaves just like the globals() function.

How do you set instance variables?

Instance variables can be declared at the class level before or after use. Access modifiers can be given for instance variables. The instance variables are visible for all methods, constructors, and block in the class. Normally, it is recommended to make these variables private (access level).

What are instance variables give an example?

An instance variable is a variable which is declared in a class but outside of constructors, methods, or blocks. Instance variables are created when an object is instantiated, and are accessible to all the constructors, methods, or blocks in the class. Access modifiers can be given to the instance variable.

Can an object be an instance variable?

An object that is created using a class is said to be an instance of that class. We will sometimes say that the object belongs to the class. The variables that the object contains are called instance variables.


1 Answers

["agreement", "user", "client"]
.map{|k| instance_variable_get("@#{k}")}
like image 132
sawa Avatar answered Sep 20 '22 06:09

sawa