0

I'm reading rails autoloading_and_reloading_constants. In the section 2.2 Class and Module Definitions are Constant Assignments

I understood the part where it says

class Project < ApplicationRecord
end 

evaluates to

Project = Class.new(ApplicationRecord)

but failed to understand this part

Thus, when one informally says "the String class", that really means: the class object stored in the constant called "String" in the class object stored in the Object constant. String is otherwise an ordinary Ruby constant and everything related to constants such as resolution algorithms applies to it.

Can someone briefly explain it?

mrzasa
  • 22,895
  • 11
  • 56
  • 94
Ankit Wadhwana
  • 325
  • 3
  • 9

1 Answers1

2

It means more or less:

  • every class is an object (as evetything in Ruby), its class is Class
  • class objects are assigned to constants, e.g. String or User
  • String as a language construct is just a constant, nothing else, it happen to have attached a class object describing textual data
  • everything that relates to loading constants relates to loading classes attached to those constants
mrzasa
  • 22,895
  • 11
  • 56
  • 94
  • 2
    *"in the class object stored in the Object constant"* is not explained here. Which means that all class definitions are stored under the `Object` namespace/class (unless otherwise specified) `String` equals `::String` equals `Object::String`, otherwise fine answer. – 3limin4t0r Sep 27 '19 at 18:54
  • 1
    Funny thing is that `Object` itself is also namespaced under `Object`. `Object == Object::Object #=> true` – 3limin4t0r Sep 27 '19 at 19:03
  • @3limin4t0r So basically the String constant is inside the object which is stored in the Object constant. – Ankit Wadhwana Sep 28 '19 at 06:49
  • 1
    @AnkitWadhwana Yes. In the part you quoted they specify it even further by saying "class object" instead of "object", but you get the point. – 3limin4t0r Sep 28 '19 at 11:35