[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Introspection question

Srijayanth Sridhar

7/14/2008 12:35:00 PM

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

3 Answers

David A. Black

7/14/2008 1:00:00 PM

0

Hi --

On Mon, 14 Jul 2008, Srijayanth Sridhar wrote:

> 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.

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

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails July 21-24 Edison, NJ
Advancing With Rails August 18-21 Edison, NJ
See http://www.r... for details and updates!

Ryan Davis

7/14/2008 9:03:00 PM

0


On Jul 14, 2008, at 05:35 , Srijayanth Sridhar 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.


Srijayanth Sridhar

7/15/2008 2:01:00 AM

0

Thank you both.

Jayanth

On Tue, Jul 15, 2008 at 2:32 AM, Ryan Davis <ryand-ruby@zenspider.com> wrote:
>
> On Jul 14, 2008, at 05:35 , Srijayanth Sridhar 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.
>
>
>