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

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.

主站蜘蛛池模板: 晋城| 开远市| 余干县| 义马市| 军事| 浦城县| 新巴尔虎左旗| 三江| 广南县| 留坝县| 闽清县| 弥渡县| 文水县| 双城市| 拜城县| 原平市| 定陶县| 商丘市| 兴仁县| 兴业县| 桃园县| 通河县| 邯郸县| 霍林郭勒市| 随州市| 曲松县| 盐池县| 新疆| 青冈县| 油尖旺区| 辛集市| 汽车| 同德县| 宝兴县| 公安县| 永胜县| 威海市| 金湖县| 读书| 泸西县| 鸡泽县|