Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Ruby, is there a way to print out all the Global variables and Constants defined / predefined?

Tags:

ruby

In Ruby, is there a way to print out all the Global variables and Constants defined / predefined?

like image 673
nonopolarity Avatar asked Jun 24 '10 05:06

nonopolarity


People also ask

How do you access global variables in Ruby?

Global Variable has global scope and accessible from anywhere in the program. Assigning to global variables from any point in the program has global implications. Global variable are always prefixed with a dollar sign ($).

Are Ruby constants global?

Ruby Constants Constants defined within a class or module can be accessed from within that class or module, and those defined outside a class or module can be accessed globally.

How do you define a constant variable in Ruby?

What is a constant in Ruby? A constant is a type of variable which always starts with a capital letter. They can only be defined outside of methods, unless you use metaprogramming. Constants are used for values that aren't supposed to change, but Ruby doesn't prevent you from changing them.


1 Answers

The global_variables method returns an array of all global variable names. To get the names of all the constants defined in a module, send constants to the relevant module. For example, to get all the constants that you can access without specifying a namespace, use Object.constants.

like image 109
Chuck Avatar answered Oct 11 '22 03:10

Chuck