How do I get the name of a variable?

0

Is there any way to get the name of a variable using metaprogramming (or reflection, I believe it's a synonym) in Ruby? I want something like nameof for C #.

I was looking for something like:

minha_variavel = "valor"
minha_variavel.name
=> "minha_variavel"

I tried to use several methods, but to no avail. I even used Object#object_id to work with the object, but I did not get the result I expected:

ObjectSpace._id2ref(minha_variavel.object_id)
=> "valor"

The ObjectSpace#_id2ref gets the instance itself, which is evaluated by its value. What I want is the name given to the object.

Is it possible? If it does not, what is impossible, if in other languages this kind of situation can be solved

    
asked by anonymous 13.02.2018 / 21:48

0 answers