How can i convert a str value to a var. name and print it !? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can i convert a str value to a var. name and print it !?

Hi there, i have just found a difficulty in converting a string value to a variable name for example : a1=1 a2=2 a3=3 a4=4 a5=5 a6=6 g="a" for k in (1..6) puts g+k.to_s end #as you see it will print a str value but i want to convert it to the variables name # to print their values

7th Feb 2019, 12:39 PM
Nakamatori
Nakamatori - avatar
1 Answer
0
Hi, you can do that using a binding method (see linked code). Keep in mind that this is not best practice. Using it for error analysis might help, but for production purposes better use arrays or other data structures solving your problem. https://code.sololearn.com/cIAw979K1Kk0/?ref=app
25th Apr 2020, 12:06 PM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar