- Comprehensive Ruby Programming
- Jordan Hudgens
- 211字
- 2021-07-02 21:13:24
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.
- 深度實踐OpenStack:基于Python的OpenStack組件開發(fā)
- PHP 從入門到項目實踐(超值版)
- Python從菜鳥到高手(第2版)
- 深入實踐Spring Boot
- Java:Data Science Made Easy
- 軟件工程
- Building an RPG with Unity 2018
- Scala編程實戰(zhàn)(原書第2版)
- Python忍者秘籍
- Arduino家居安全系統(tǒng)構(gòu)建實戰(zhàn)
- Hands-On Nuxt.js Web Development
- Learning Grunt
- Java高手是怎樣煉成的:原理、方法與實踐
- Google Adsense優(yōu)化實戰(zhàn)
- Responsive Web Design with jQuery