From: Mark Hubbart Date: 2004-08-10T01:43:45+09:00 Subject: Re: stupid question: Object#name On Aug 9, 2004, at 7:21 AM, Benny wrote: > Robert Klemme wrote: > >> >> "Benny" schrieb im Newsbeitrag >> news:2npa39F2ppenU1@uni-berlin.de... >>> hi all, >>> >>> its time for stupid questions again :) >>> >>> is there a way to get the name of an object, e.g. >>> >>> test = MyClass.new >>> test.name #=> "test" >> >> That's not the name of the object but the name of a variable that >> references the object. What would you expect to be the name in this >> case: >> >> test = MyClass.new >> t2 = test >> # name? >> > ok, see my other posting: > > "substitute Object#name with Object#names > in my first posting" > so it would be > > test = MyClass.new > t2 = test > t2.names #=> ["t2", "test"] > test.names #=> ["t2", "test"] This isn't exactly what you want, but it's as close as you can come, currently, in ruby. Without a whole bunch of ugliness, anyway. def names(obj, bind) vars = eval("local_variables", bind) vars.select{|var| eval(var, bind).id == obj.id} end ==>nil a = 2.3 ==>2.3 b = a ==>2.3 c = 2.3 ==>2.3 names(a, binding) ==>["a", "b"] names(c, binding) ==>["c"] names(2.3, binding) ==>[] Now I suppose it would be possible to do something like this (untested): def add_binding(bind) $stored_bindings ||= [] $stored_bindings << bind end def do_something add_binding(bind) #... do other stuff end then... def find_names(obj) variable_names = [] $stored_bindings.each do |bind| vars = eval("local_variables", bind) variable_names << vars.select{|v| eval(v, bind).id == obj.id} end end Now, you can call find_names on any object that is in a binding that you have indexed. Still, though, for tis to be a complete solution, you woiuld have to come up with a way to index the @vars and @@vars, $globals and CONSTANT_VARS, as well. I have a feeling that all this infrastructure would cause a considerable slowdown, though. >>> I know we have it for classes (and yes: I could use a subclass >>> instead) >> >> That's a special case. > yes I know: class names are constants > >> Consider also: >> >>>> class Foo;end >> => nil >>>> Foo >> => Foo >>>> f = Foo >> => Foo >>>> f.name >> => "Foo" >>>> f == Foo >> => true >> > I dont get you here :( > I think as names of classes a constants and names of variables are > symbols > they are a entirely different thing (in the way ruby handles it) > I only made the statement with the subclass to get no such answer from > the > list ("use a subclass if you need names"). and as I didnt want to use > constants I also didnt want to use a subclass :) > >>> but is there a way to do it with objects? >> >> No. > from my other posting: > > "I think in the kernel we have symbols attached to object-ids (do we?) > so why not have a method to show us the symbols to a corresponding > object_id?" If I understand correctly, they are stored that way in the internal symbol table. That symbol table is inaccessible, though. Once you are in the runtime, the name -> obj correlations are spread out in various constructs. Some are easier to get at than others; it's easy to lookup a constant, but local variables are truely a pain. >>> btw. why didn't matz make Class and Object be the same thing? >> >> Err... Because they are not the same concept. > if they behave mostly the same way in ruby > it might be reflected behind the scenes. > are the differences in the abilities of Object and > Class so big that they legitimate hard distinction in the > interpreter? or are we loosing something if Object and Class > is based on the same fundamentals apart from syntactical restrictions > and the separated name spaces (constants vs. symbols)? Class is a template for creating objects. Object is a subclass of class. This is a fairly common OO topic, and there is a pretty good description of classes and objects here: http://www.visibleworkings.com/little-ruby/Chapter3.pdf >> Object is an instance of >> Class (as Class is also, which sometimes confuses people). Object >> defines >> everthing an instance is capable of (ok, together with Kernel, but >> that >> contains mostly methods that one uses as functions). Could be that >> the >> self referenciality confused you - sometimes it's hard to grasp. :-) > > I think its not confusing me on the contrary: I even vote for > narrowing the > differences. > > >> Maybe you should get yourself a book about programming languages >> concepts >> and / or about OO in special. Just an idea. > I read the whole pragmatic programmer (1st ver.) and "Programmieren mit > Ruby" /R�hrl/Schmiedl/Wyss several times and I thought it would be > sufficient. but perhaps you have a good recommendation for a pure OO > book. > > kind regards, > > benny >