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

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.

主站蜘蛛池模板: 英超| 琼海市| 水城县| 杂多县| 邵武市| 新竹县| 北票市| 陈巴尔虎旗| 唐河县| 常宁市| 北宁市| 麻栗坡县| 黄冈市| 九寨沟县| 望城县| 城固县| 宁德市| 五家渠市| 东平县| 富源县| 杭锦旗| 南京市| 平利县| 讷河市| 广饶县| 江津市| 任丘市| 伊春市| 栾城县| 珲春市| 长沙县| 宝丰县| 宁强县| 德州市| 二连浩特市| 巴林右旗| 沙田区| 亚东县| 长寿区| 栾川县| 门源|