Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Patterns > Optimize Access to Global Constants

Optimize Access to Global Constants

In Ruby, references to constants are resolved at runtime, each time you access a constant. This is necessary, as constants can be added to or removed from any namespace dynamically. Moreover, the interpreter cannot replace the constant name with the constant value, as the value the named constant refers to can be changed as well.

Consequently, accessing a global constant, that is, a constant defined at the top level inside the class Object, is more costly than accessing a constant defined within the current or a nearby scope. For deeply nested classes, possibly also including a number of modules, you can more quickly resolve a constant reference by prefixing the constant with the namespace operator ::.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint