# Introspection question

**URL:** <https://www.ruby-forum.com/t/introspection-question/143011>\
**Category:** Ruby\
**Created:** [July 14, 2008, 2:44pm UTC](https://www.ruby-forum.com/t/introspection-question/143011 "2008-07-14T14:44:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [July 14, 2008, 2:44pm UTC](https://www.ruby-forum.com/t/introspection-question/143011/1 "2008-07-14T14:44:42Z")

</div>

Hello,

Let us say we have a hash:

a = Hash.new

Can I do something of this sort:

class Hash  
def who\_am\_i?  
“#{self.symbol}”  
end  
end

Where self.symbol gives me “a”?

I basically want the variable name. I looked a bit and saw that you  
could intern a symbol from a string, but is there an easy way to get  
the name of a variable from within the object itself? I am writing a  
to\_xml method for hashes and it would be immensely helpful if I can  
use the name of the variable as the root element.

Thank you,

Jayanth

---

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [July 14, 2008, 3:05pm UTC](https://www.ruby-forum.com/t/introspection-question/143011/2 "2008-07-14T15:05:10Z")

</div>

Hi –

On Mon, 14 Jul 2008, Srijayanth S. wrote:

> “#{self.symbol}”  
> end  
> end
> 
> Where self.symbol gives me “a”?
> 
> I basically want the variable name. I looked a bit and saw that you  
> could intern a symbol from a string, but is there an easy way to get  
> the name of a variable from within the object itself? I am writing a  
> to\_xml method for hashes and it would be immensely helpful if I can  
> use the name of the variable as the root element.

The hash has no knowledge of how many identifiers refer to it.  
Consider this:

a = Hash.new  
b = a  
c = b

The three identifiers are in exactly the same relation to the hash.  
What is the hash supposed to think?

David

---

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [July 14, 2008, 11:08pm UTC](https://www.ruby-forum.com/t/introspection-question/143011/3 "2008-07-14T23:08:06Z")

</div>

On Jul 14, 2008, at 05:35 , Srijayanth S. wrote:

> Can I do something of this sort:
> 
> class Hash  
> def who\_am\_i?  
> “#{self.symbol}”  
> end  
> end
> 
> Where self.symbol gives me “a”?

I totally agree with David but will take his idea a bit further:

What you’re asking has what we call a “bad design smell”… I have  
never seen a legitimate need for what you’re asking for all the good  
reasons that David mentioned. You need to look at the design of your  
code and ask what you’re really trying to accomplish. Something is  
currently wrong with the design you’re currently attempting.

---

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [July 15, 2008, 4:06am UTC](https://www.ruby-forum.com/t/introspection-question/143011/4 "2008-07-15T04:06:33Z")

</div>

Thank you both.

Jayanth
