Stupid question: Object#name

Vincent Isambart wrote:

About the class name, the name of the class is not necessarily the name
of the constant that references the name. Example:
class Foo
end
Bar = Foo
Foo = 0

Then you cannot do Foo.new, but Bar.name returns "Foo".
I think in fact, when the class does not exist, Ruby creates the class,
and gives it the name that is after "class". It then gives to a
constant of the same name the value of the class.

If you try to add a method to Foo, it does not work :
class Foo
   def a() end
end
-> TypeError: Foo is not a class

You can still add a method to the class created before :
class Bar
   def a() end
end

and Bar.field still returns "Foo".
So the name field of class is just a name that does not necessarily
means anything.

so if I get you right, when creating a new class three things happens:
1. creation of a new class-object
2. setting the attribute "name" of the class to the given class name
3. creation of a constance with the given class name as reference to the new
class-object

tant mieux!

then we could do the same thing everytime an object is created ( = set with
"=")!
1. creation of the object, registering its relation to its class...etc
2. setting the attribute "name" of the object to the given object name, if
not given: nil

4.name #=> nil
"test".name #=> nil
test = Test.new
test.name #=> "test"
Test.new.name #=> nil
test2 = test
test2.name #=> "test"
test2.name = "test3"
test2.name #=> "test3"

name would be just another attribut of Object just as is for Class
(and as I initially thought of)

I hope what I said was understandable ^o^

surely it was.

Regards,
Vincent Isambart

regards,
benny

Benny wrote:

so if I get you right, when creating a new class three things happens:
1. creation of a new class-object
2. setting the attribute "name" of the class to the given class name
3. creation of a constance with the given class name as reference to the new
class-object

tant mieux!

then we could do the same thing everytime an object is created ( = set with
"=")!

We *could* in theory do that.

But you already see that it does not work 100% with class names.
And nothing in Ruby actually *relies* on this working.

What if we have an array of 100 objects? Each has no other name
than foo[73] or whatever. Do we really want to store the string
"foo[73]" somewhere? What if we do a delete, or a sort? Now these
have changed. Do we really want to update all these in realtime?

What about Hashes?

What about Structs and class members that have no other names
except complex names like Foo::Bar::myclass.mymember ?

My opinion is: You would be better off abandoning this idea, or if
you really, really need this functionality, implement something
of your own in Ruby.

Hal