But this is my try. I am looking for any method is available in
Ruby,into which say if I pas n1.object_id or #Sample:0x13bc648,I will
get back in return the local variable name say here it is - n1 ?
A variable has an identifier ( in your case, n1 ) and a reference to an
object ( Sample:0x13bc648 ) so when you say “n1” , it knows which object
it
references and returns that. As far as I know Ruby doesn’t keep track of
reference counts of an object and the object has no way of finding out
which variable references itself.
But one inefficient way to achieve what you want would be to iterate
through all the local variables and print the names of all local
variables
that has a particular object ID.
a = b = “hello world”
local_variables.each {|x| puts x if (eval x.to_s).object_id ==
a.object_id}
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.