Exploring Ruby's Global Constants and Variables - FastRuby.io | Rails Upgrade Service

14-Dec-2023 1133
By default, Ruby defines many constants and global variables that can be used in the code to get information about the current state of the application and the runtime. In this article we’ll go over most of them to understand what they are and what information we can set and get to simplify our scripts or to debug problems.Global ConstantsIn Ruby, a constant encompasses many concepts: it can be a class, a module, or a primitive value. Global constants are actually defined as constants of the Object class, so RUBY_PLATFORM and Object::RUBY_PLATFORM are equivalent.If we execute Object.constants we can get a list of all the global constants. This includes classes (like the Exception class), modules (like the JSON module), and others like RUBY_PLATFORM or ARGV that are primitives (a string and an array, respectively).Useless fact: Object is a global constant, so it’s defined as a constant inside Object itself, so Object::Object::Object is equivalent to Object.Let’s explore some of them.
Use coupon code:

RUBYONRAILS

to get 30% discount on our bundle!
Prepare for your next tech interview with our comprehensive collection of programming interview guides. Covering JavaScript, Ruby on Rails, React, and Python, these highly-rated books offer thousands of essential questions and answers to boost your interview success. Buy our 'Ultimate Job Interview Preparation eBook Bundle' featuring 2200+ questions across multiple languages. Ultimate Job Interview Preparation eBook Bundle