Ákos Vandra-Meyer
Ákos Vandra-Meyer

Reputation: 2175

Associating a class object with a variable changes the class object

Please explain how this can happen:

a = Class.new(StandardError).new
# => #<#<Class:0x007fa3af1d24a0>: #<Class:0x007fa3af1d24a0>>
a.class.name
# => nil
AError = a.class
# => AError
a.class.name
# => "AError"

Is the class object assignment handled as a special case in the language/vm? How is the class object aware that it has or has not been assigned to a constant?

Upvotes: 0

Views: 45

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230461

Yes, this is intended behaviour / special case. When you create a class with Class.new, it doesn't have a name initially. It copies the name of the first constant it's assigned to.

How is the class object aware that it has or has not been assigned to a constant?

Ruby VM knows everything.

Upvotes: 4

Related Questions