官术网_书友最值得收藏!

Constants

If you're coming from other programming languages, Ruby handles constants differently than what you may be used to. Constants, in general, take values that do not change through the entire application. The syntax is to use all capital letters while naming your constant so that the application knows how to handle it. For example, to set a constant to hold a baseball team, you would declare it this way:

TEAM = "Angels" 

Typically, other programming languages will not allow you to change the value of TEAM. However, Ruby does not hold you back, and it takes the last value assigned to the constant. In the preceding example, I can change its value to:

TEAM = "Athletics" 

Other programming languages would either throw an error or would print the Angels value. However, Ruby prints the Athletics value because that is the last value assigned to the TEAM variable. Also, it gives a warning message which says that the constant was already initialized and was changed because changing a constant is considered a poor programming practice. However, it still lets you make that change and follows the Ruby convention of trusting the developer to make the right programming decision. So, be careful while using constants in Ruby since they can be overridden.

主站蜘蛛池模板: 普洱| 商城县| 英山县| 霞浦县| 弥渡县| 延吉市| 泽普县| 武陟县| 蒙自县| 宁阳县| 夏津县| 边坝县| 左权县| 蕲春县| 师宗县| 连云港市| 乌兰县| 苍山县| 正阳县| 安国市| 虞城县| 中山市| 汾西县| 乳源| 伊吾县| 四川省| 河池市| 米泉市| 海兴县| 石景山区| 泰兴市| 隆尧县| 商水县| 沧州市| 纳雍县| 贵溪市| 盐边县| 石楼县| 崇文区| 武穴市| 临海市|