From: Dan Doel Date: 2004-04-05T01:31:30+09:00 Subject: Re: Variable names Let me see if I can do better this time. You read the expression: a.foo And you see #foo being called on the object called 'a'. The interpreter does something like this: 1) I see that I need to find an object called 'a' 2) I look in my symbol table and find the appropriate 'a' entry and follow it to the object it points to. 3) I call the #foo method of the object I have. Note that in 3 there is no reference to 'a'. In fact, the object you have could be the result of some more complicated expression as in the case (1 + 2).foo In that case, the process is more like: 1) Get the object that's the result of evaluating (1 + 2) 2) Call #foo on that object Now, you're inside #foo, so you have the object that 'a' pointed to, and you want to retrieve :a from it. However, in this process, once you have the object, you throw away the variable name or expression that it came from because you don't need it anymore. If you had access to the symbol table, you could look through it and find all symbols that lead to the object you have. But it's possible that it has more than one name in the symbol table, so although you could find _a_ name for that object, you wouldn't necessarily be able to find the name you want. Also, in the (1 + 2) case, the object doesn't necessarily have a name in the symbol table, because (1 + 2) wasn't assigned to any variable, and the symbol table only stores variable names. It doesn't matter anyway since Ruby doesn't give you access to the symbol table. What you want (more or less) is the expression that most recently corresponded to the object, but the interpreter doesn't store that information because it's useless 99% of the time. That is a technical explanation of why you can't do it. If you want a more abstract description try this: A variable name is not part of an object. An object is a chunk of memory, and a variable name is a memory address. The chunk of memory doesn't know what its address is, though; it's just a sequence of 1's and 0's. Analogy: I am me. Other people call me by some name (Dan, Daniel, Dolio, etc.). I don't call myself by any of those names, and I don't know all the names that people use to think about me. I'd have to ask them all to find that information out. In some sense, my name isn't intrinsically part of me, it's just a label people use to refer to me. It's external, so I don't know about or use it. Was any of this helpful? If not I can try again, but I don't know how I can clarify much better, either in concrete or abstract terms. - Dan