From: Chris Shea Date: 2008-09-28T08:34:23+09:00 Subject: Re: Variable name to string On Sep 27, 4:50 pm, reuben doetsch wrote: > [Note:  parts of this message were removed to make it a legal post.] > > I would like to get the name of the variable in string form, any way this is > possible? > > var = 5 > puts var.foo  #Want this ti put var > > i looked into trying to do var.object_id.id2name but some reason this gave > me nil, since if it is bugged or object_id is not returning the correct > object id. > > Reuben I don't think I quite understand why you need this. But this does what you're asking for (although, it's a little hacky). See section 8.6 in The Ruby Programming Language by Flanagan and Matz for more. cms@mvb cat t.rb class Object def get_name line_number = caller[0].split(':')[1].to_i line_exectued = File.readlines(__FILE__)[line_number-1] line_exectued.match(/(\S+)\.get_name/)[1] end end abracadabra = 3 puts abracadabra.get_name cms@mvb ruby t.rb abracadabra [~] cms@mvb Although, since you're able, apparently, to ask for var.foo, I'd suggest just putting var in quotes instead of appending '.foo'. Chris