# Does an instance know his own name?

**URL:** <https://www.ruby-forum.com/t/does-an-instance-know-his-own-name/67399>\
**Category:** Ruby\
**Created:** [September 3, 2006, 10:48pm UTC](https://www.ruby-forum.com/t/does-an-instance-know-his-own-name/67399 "2006-09-03T22:48:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![glub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@glub](https://www.ruby-forum.com/u/glub)\
**Post date:** [September 3, 2006, 10:48pm UTC](https://www.ruby-forum.com/t/does-an-instance-know-his-own-name/67399/1 "2006-09-03T22:48:06Z")

</div>

If a class has built checks and raises an exception if it finds  
something wrong, how can I tell which instance had the error? I know  
there’s an object\_id, but can I know the name of the objects own  
instance variable?

huey = Nephew.new  
duey = Nephew.new  
luey = Nephew.new

duey.raise\_error\_on\_purpose\_just\_for\_fun #-\> “Nephew ‘duey’ raised an  
error”

Also, can an instance know his ‘path’ from the top-level?

class Donald

def initialize  
@huey = Nephew.new  
@duey = Nephew.new  
@luey = Nephew.new  
end

end

Donald1 = Donald.new  
Donald2 = Donald.new

Donald1.huey.raise\_error\_on\_purpose\_just\_for\_fun  
#-\> “Nephew Donald1.huey raised an error”

Thanks,

---

<div class="post-metadata">

**Author:** ![glub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@glub](https://www.ruby-forum.com/u/glub)\
**Post date:** [September 4, 2006, 12:10am UTC](https://www.ruby-forum.com/t/does-an-instance-know-his-own-name/67399/2 "2006-09-04T00:10:01Z")

</div>

Geoff Barnes wrote:

> If a class has built checks and raises an exception if it finds  
> something wrong, how can I tell which instance had the error? I know  
> there’s an object\_id, but can I know the name of the objects own  
> instance variable?
> 
> Also, can an instance know his ‘path’ from the top-level?

No and no (at least, no sensible way). You are dealing with  
symbolic names ‘huey’ versus actual references and objects.

The ‘proper’ way is to have the object know its name:

huey = Nephew.new ‘Huey’

> Thanks,

---

<div class="post-metadata">

**Author:** ![glub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@glub](https://www.ruby-forum.com/u/glub)\
**Post date:** [September 4, 2006, 2:44pm UTC](https://www.ruby-forum.com/t/does-an-instance-know-his-own-name/67399/3 "2006-09-04T14:44:09Z")

</div>

On 06-09-03, at 23:10, Geoff Barnes wrote:

> If a class has built checks and raises an exception if it finds  
> something wrong, how can I tell which instance had the error? I know  
> there’s an object\_id, but can I know the name of the objects own  
> instance variable?

Instances don’t have names.

> huey = Nephew.new  
> duey = Nephew.new  
> luey = Nephew.new

These are examples of binding an instance to a particular key (think  
of it in terms of a dictionary/hashtable), and not naming the  
instances of Nephew: huey, duey and luey.

---

<div class="post-metadata">

**Author:** ![glub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@glub](https://www.ruby-forum.com/u/glub)\
**Post date:** [September 5, 2006, 2:32pm UTC](https://www.ruby-forum.com/t/does-an-instance-know-his-own-name/67399/4 "2006-09-05T14:32:25Z")

</div>

You can identify an object by its object\_id. Variables are just  
references to an object with a convenient name for your use. If you  
want to give an object an actual name, you have to pass it in as a  
parameter on construction.

---

<div class="post-metadata">

**Author:** ![glub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@glub](https://www.ruby-forum.com/u/glub)\
**Post date:** [September 5, 2006, 5:09pm UTC](https://www.ruby-forum.com/t/does-an-instance-know-his-own-name/67399/5 "2006-09-05T17:09:01Z")

</div>

Geoff Barnes wrote:

> huey = Nephew.new  
> duey = Nephew.new  
> luey = Nephew.new
> 
> duey.raise\_error\_on\_purpose\_just\_for\_fun #-\> “Nephew ‘duey’ raised an  
> error”

And what would happen if you did:  
huey = Nephew.new  
duey = huey  
luey = duey  
some\_array = [huey]  
huey = nil

duey.raise\_error #=\> “Nephew…uhm…duey, luey, some\_array[ 0  
]?..raised an error.”

A variable doesn’t hold an object, it holds a reference to it. Many  
variables (local, global, instance, and class) can all refer to the  
same object.

If you have a symbolic name that is unique to an instance, pass that  
name in when creating the instance.

---

<div class="post-metadata">

**Author:** ![glub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@glub](https://www.ruby-forum.com/u/glub)\
**Post date:** [September 5, 2006, 2:44pm UTC](https://www.ruby-forum.com/t/does-an-instance-know-his-own-name/67399/6 "2006-09-05T14:44:29Z")

</div>

On Sep 5, 2006, at 8:30 AM, Timothy G. wrote:

> > huey = Nephew.new  
> > duey = Nephew.new  
> > luey = Nephew.new

It is sort of possible to find out someone’s name. The following of  
course, is easily breakable.  
% cat names.rb  
class Nephew  
def name(&block)  
names\_objects = eval(“local\_variables.inject({}){ |h, k| h[k] =  
eval(k); h}”, block)

```
 names_objects.find { |k, v| v.object_id == object_id }.first

```

end  
end

huey = Nephew.new  
duey = Nephew.new  
louie = Nephew.new

puts huey.name{}

% ruby names.rb  
huey
