# Re: Is it possible from a class to grab the name of the object instance?

**URL:** <https://www.ruby-forum.com/t/re-is-it-possible-from-a-class-to-grab-the-name-of-the-object-instance/138128>\
**Category:** Ruby\
**Created:** [May 20, 2008, 2:02pm UTC](https://www.ruby-forum.com/t/re-is-it-possible-from-a-class-to-grab-the-name-of-the-object-instance/138128 "2008-05-20T14:02:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubisher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eb9ed0/32.png) [@rubisher](https://www.ruby-forum.com/u/rubisher)\
**Post date:** [May 20, 2008, 2:02pm UTC](https://www.ruby-forum.com/t/re-is-it-possible-from-a-class-to-grab-the-name-of-the-object-instance/138128/1 "2008-05-20T14:02:49Z")

</div>

Hello Marcin,

> self.class.to\_s should work

Sorry this return me the class name itself not the instance name?

e.g.  
#!/usr/bin/env ruby

class Investment  
def initialize  
puts “I’am ALIVE!”  
iname=self.class.to\_s  
puts “Instance Name: #{iname}”  
end  
end

\_Invest = Investment.new

puts \_Invest.class

give me:  
I’am ALIVE!  
Instance Name: Investment  
Investment

and not “Instance Name: \_Invest” as am looking for.

Tx,  
r.

---

<div class="post-metadata">

**Author:** ![rubisher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eb9ed0/32.png) [@rubisher](https://www.ruby-forum.com/u/rubisher)\
**Post date:** [May 21, 2008, 11:33am UTC](https://www.ruby-forum.com/t/re-is-it-possible-from-a-class-to-grab-the-name-of-the-object-instance/138128/2 "2008-05-21T11:33:40Z")

</div>

2008/5/20 rubisher [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> class Investment
> 
> give me:  
> I’am ALIVE!  
> Instance Name: Investment  
> Investment
> 
> and not “Instance Name: \_Invest” as am looking for.

There is no such thing as an instance name - unless you define a  
property of your class called “name”. Each instance can be referenced  
by any number of variables including 0 - there is simply not _the_  
name. As Inaki pointed out.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![rubisher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eb9ed0/32.png) [@rubisher](https://www.ruby-forum.com/u/rubisher)\
**Post date:** [May 23, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/re-is-it-possible-from-a-class-to-grab-the-name-of-the-object-instance/138128/3 "2008-05-23T23:01:14Z")

</div>

Robert K. wrote:

> > puts \_Invest.class
> > 
> > give me:  
> > I’am ALIVE!  
> > Instance Name: Investment  
> > Investment
> > 
> > and not “Instance Name: \_Invest” as am looking for.
> 
> There is no such thing as an instance name - unless you define a  
> property of your class called “name”.  
> Ok what I did.  
> The idea was that as far as ruby is interpreded, it have to save  
> variable name somewhere in clear with its id and for  
> debuging reason it could be accessible somehow. But ok it’s not possible  
> and for me it’s just nice to know.

> Each instance can be referenced  
> by any number of variables including 0 - there is simply not _the_  
> name. As Inaki pointed out.
> 
> Kind regards
> 
> robert

Tx to all for feedback,  
r.

---

<div class="post-metadata">

**Author:** ![rubisher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eb9ed0/32.png) [@rubisher](https://www.ruby-forum.com/u/rubisher)\
**Post date:** [May 24, 2008, 10:50pm UTC](https://www.ruby-forum.com/t/re-is-it-possible-from-a-class-to-grab-the-name-of-the-object-instance/138128/4 "2008-05-24T22:50:15Z")

</div>

Phillip G. wrote:

> Well, you could modify your MyClass#initialize method to do something  
> similar.
> 
> Add a global variable or something similar, and add the object to this  
> variable (for example a Hash, with the variable name as key, and the  
> object’s class as value).

Good, very good idea, I will investigate.

> Though, you’d have to intercept the assignment somehow to grab the  
> variable name.

Thanks a lot,  
r.

---

<div class="post-metadata">

**Author:** ![rubisher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eb9ed0/32.png) [@rubisher](https://www.ruby-forum.com/u/rubisher)\
**Post date:** [May 24, 2008, 3:23am UTC](https://www.ruby-forum.com/t/re-is-it-possible-from-a-class-to-grab-the-name-of-the-object-instance/138128/5 "2008-05-24T03:23:02Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

rubisher wrote:

| The idea was that as far as ruby is interpreded, it have to save  
| variable name somewhere in clear with its id and for debuging reason it  
| could be accessible somehow. But ok it’s not possible and for me it’s  
| just nice to know.

Well, you could modify your MyClass#initialize method to do something  
similar.

Add a global variable or something similar, and add the object to this  
variable (for example a Hash, with the variable name as key, and the  
object’s class as value).

Though, you’d have to intercept the assignment somehow to grab the  
variable name.

HTH,

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

Format a program to help the reader understand it.  
~ - The Elements of Programming Style (Kernighan & Plaugher)  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkg3bS8ACgkQbtAgaoJTgL/bPwCfRra/JfzsjjD5XS9cRXdB7Ede  
RRUAn3eYv1QZQnWHIIzVixr1hm1eDwUR  
=vkQ1  
-----END PGP SIGNATURE-----
