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

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.

主站蜘蛛池模板: 和平县| 抚州市| 南乐县| 乐安县| 平江县| 枣阳市| 霍邱县| 家居| 怀来县| 宕昌县| 通江县| 沁阳市| 宁都县| 武穴市| 常宁市| 武义县| 侯马市| 习水县| 遂川县| 贺兰县| 宁化县| 志丹县| 成武县| 大关县| 固安县| 鱼台县| 皮山县| 长垣县| 通城县| 平安县| 岳普湖县| 同德县| 白银市| 松江区| 林口县| 迁西县| 塘沽区| 行唐县| 山阴县| 腾冲县| 朝阳区|