Suppose in Ruby I have a = "value"; arr1 = [a, b, c];
and arr2 = [a, d, e];
Is there some reflective programming technique allowing me to say:
What are all the objects which have a reference to
a.object_id
?
and getting as an answer something like:
object_id:123123 (Array)
object_id:234234 (Array)
There is
ObjectSpace
, commonly used for this kind of queries. Please note, that the code above will produce a lot of garbage output in IRB/Pry since those introduce their own bindings etc.This code has a side effect: it actually checks whether an array includes a variable by value. That said, plain
[42]
will be counted as well and you are probably interested in making more sophisticated check insideselect
.But generally speaking, the answer to the question “what to use to query the global object space” is that linked in the very beginning of my answer:
ObjectSpace
.