Get Variable Name from object_id

E

Evan Moseman

If I am perusing through the each_object loop and find the objects I'm
interested in, how can I get their variable names?

Thanks!
 
T

Tim Hunter

Evan said:
If I am perusing through the each_object loop and find the objects I'm
interested in, how can I get their variable names?

Thanks!

You can't. Variables are just labels for objects. What if you have
something like this:

foo = MyClass.new
bar = foo

What's the variable name of the object? What about

ary = []
ary << bar

Now what's the variable name? What about this

def f(v)
puts v
end

f(ary[0])

Now what's the variable name?
 
T

Todd Benson

Evan said:
If I am perusing through the each_object loop and find the objects I'm
interested in, how can I get their variable names?

Thanks!

You can't. Variables are just labels for objects. What if you have
something like this:

foo = MyClass.new
bar = foo

What's the variable name of the object? What about

ary = []
ary << bar

Now what's the variable name? What about this

def f(v)
puts v
end

f(ary[0])

Now what's the variable name?

I was going to reply in the same way, but I suppose it would be
interesting to call up names for objects if the names exist for the
object space (as nil or array for each object maybe?). Like
Object#references or something like that.

Todd
 
B

botp

interested in, how can I get their variable names?

thinking about the subject, i think you want to refer to the object fr
a given object id, mayb try

ObjectSpace#_id2ref

kind regards -botp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top