From: Jamis Buck Date: 2004-04-05T01:10:17+09:00 Subject: Re: Variable names David, I think I'll have to side with you on this. Experts, in my experience, have a tendency to answer the literal question that was asked, without trying to understand and clarify the issues underneath. I know I've fallen into that trap before. However, don't assume it to be a conversational mistake: I've noticed that it tends to happen only in electronic media; the same person would not necessarily have the same conversational style person-to-person. I have to admit that I have not followed this thread. However, Looking back in the archive I think I'll take a stab and answering your question. > Is there any way to access the name of a given variable instance from > within it? In other words, for any object x, is there C function to > which I can pass self to determine that the program calls it 'x'? I have a strong desire to say "no", here. That's the easy answer. :) The more difficult answer requires some understanding of Ruby's execution environment. First of all, there is no guarantee that the object that is recieving the message is bound to a variable at all. Consider: puts Object.new.to_s The object recieving the "to_s" message is not bound to a variable. Another scenario is that an object may be bound to multiple variables: a = b = Object.new puts b.to_s Your question, if I understood it, is whether or not the "to_s" method in the example above would be able to determine that it was invoked via the binding of 'self' to 'b'. The answer to that is "no". It *may* be possible to identify that it could have been bound to either 'a' or 'b' (it would require some introspection of the local execution environment of the caller, which may or may not be possible), but you cannot state for certain one way or another in Ruby. I hope that helped to answer your question. What is more, I hope I didn't duplicate another post in this thread that I failed to read. :-D - Jamis David King Landrith wrote: > Too clever by half, Charles: > > Just the same, you've hit upon why I seldom find this list to be very > useful. > > > On Apr 4, 2004, at 10:41 AM, Charles Miller wrote: > >> "Where can I find this fish's bicycle?" >> >> "Fish don't have bicycles." > > > The question appears to be enquiring about modes of travel. Thus, a > reasonable answer might point the user in the right direction to > understand modes of fish travel. Even if you don't understand what the > questioner is trying to get at, if you still desire to offer some > answer, then you should at least request clarification. > > The answer you've proposed above offers as much illumination as the > following exchange: > > Q: "Why do fish hav fines?" > > A: "Fish don't 'hav' fines." > > Q: "OK. Why do 'fishe' have fins?" > > A: "'Fishe' don't have fins'." > > etc. > >> "But they must have something like it. Can I find their cars?" >> >> "No, they don't have cars." >> >> "But they have to get to work somehow!" >> >> "They do?" > > > If you really think that this is a reasonable type of conversation, then > you may want to find and read some book on how to engage in fruitful > conversation. I understand that there are several good ones out there. > Books that have received good reviews on Amazon include > _Conversationally Speaking : Tested New Ways to Increase Your Personal > and Social Effectiveness_ by Alan Garner, or _How to Start a > Conversation and Make Friends : Revised and Updated_ by Don Gabor. I'm > not offering this to be insulting or glib, if you think that this type > of exchange is appropriate then you really may find them to be at least > somewhat helpful. (For the record: I have no relationship to these > books at all, and I have nothing to gain by mentioning them.) > >> "You guys are so unhelpful. I still don't know where I can find the >> bicycles." > > > Actually it's more like the following: > > After 2 days and several emails, I still have no idea how the characters > are getting evaluated and resolved by the interpreter and at what point > these resolved or evaluated characters are disposed of in favor of the > objects that they represent. Moreover, I still have no idea if or why > this question may reflect a category mistake. > > I have no experience in writing compilers/interpreters, and I don't > believe that I should have to in order to learn about such topics. > > Best regards, > > Dave > > ------------------------------------------------------- > David King Landrith > (w) 617.227.4469x213 > (h) 617.696.7133 > > One useless man is a disgrace, two > are called a law firm, and three or more > become a congress -- John Adams > ------------------------------------------------------- > public key available upon request > > > . > -- Jamis Buck jgb3@email.byu.edu http://www.jamisbuck.org/jamis ruby -h | ruby -e 'a=[];readlines.join.scan(/-(.)\[e|Kk(\S*)|le.l(..)e|#!(\S*)/) {|r| a << r.compact.first };puts "\n>#{a.join(%q/ /)}<\n\n"'