From: Robert Klemme Date: 2004-08-09T23:41:20+09:00 Subject: Re: stupid question: Object#name "Benny" schrieb im Newsbeitrag news:2npfesF2oe2sU1@uni-berlin.de... > 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"] No good either. > >> 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 :( The message is "even an ordinary variable can reference a class instance". > 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) There's much less difference than you think. Both are references and with respect to that they are totally interchangable. The one major difference is that constants can only be assigned once - more precisely, Ruby warns you if you try more assignments, but it doesn't prevent them: >> Foo = Object.new => # >> Foo = Object.new (irb):2: warning: already initialized constant Foo => # > 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?" No, as we said already: it's a one way street. And it's not "in the kernel" but in the current binding. > >> 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. They don't. Instances of Object and Class are totally differnt. Regard this >> o1 = Class.new => # >> o2 = o1.new => #<#:0x10188d70> >> o2.class => # >> p1 = Object.new => # >> p2 = p1.new NoMethodError: undefined method `new' for # from (irb):6 You can create instances of a class but not of an object. > are the differences in the abilities of Object and > Class so big that they legitimate hard distinction in the > interpreter? Definitely! See above. > 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)? > > > 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. IMHO you don't *see* 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. Bertrand Meyers OOSC is regarded one of the standards: http://www.amazon.com/exec/obidos/tg/detail/-/0136291554 But you shouldn't worry if OO is not immediately clear to you. Sometimes it takes some time. Regards robert