简体   繁体   中英

Rails constant assignment

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?

It means more or less:

  • every class is an object (as evetything in Ruby), its class is Class
  • class objects are assigned to constants, eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM