An object has no idea what variable it may or may not be stored in, and in many cases this is a completely meaningless concept.
What would you expect to happen here?
ref = Object.new
another_ref = ref
@ref = another_ref
hash = { key: @ref }
array = [ hash[:key] ]
array[0].my_val
# => ...?
There are so many ways to reference an object. Knowing which name is being used is irrelevant.
In general terms, variables are just references that are given an arbitrary name that shouldn't matter to the object in question.
What you can do is provide context:
my_var = "test"
my_var.my_val(:my_var)
# => my_var="test"
Implemented as:
def my_var(name)
"#{name}=#{self.inspect}"
end
You can also roll this up a little and be clever about it:
def dump_var(name)
"%s=%s" % [ name, instance_variable_get(:"@#{name}").inspect ]
end
Then:
@foo = "test"
dump_var(:foo)
# => foo="test"