What does Ruby constant mean?

后端 未结 6 1684
走了就别回头了
走了就别回头了 2020-12-18 07:58

What does Ruby constants really mean? The following code doesn\'t show any \'constant\' attribute. The warning is there, but I still get to change what A refers to.

6条回答
  •  别那么骄傲
    2020-12-18 08:18

    That's right, constants are just like variables in ruby, but you get a warning if you change them.

    Also, there's one difference with mere variables: You can access constants even if they are defined inside another class or module, for example given this snippet:

    module Constants
      PI = 3,1415
      other = "variable"
    end
    

    You can reach PI doing Constants::PI while Constants::other will not work.

提交回复
热议问题