Ruby initializes built-in global variables when the interpreter starts. They remain available across scope changes, including inside methods defined within classes. section 5.2.1
Common built-in globals expose runtime and loading information:
- $0 contains the startup file name, $: contains directories Ruby searches when loading external files, and $$ contains the Ruby process ID. section 5.2.1
- Use require "English" for friendlier aliases such as $INPUT_LINE_NUMBER for $. and $PID for $$; $LOAD_PATH is available without requiring it. section 5.2.1
$gvar = "I'm a global!"
class C
def examine_global
puts $gvar
end
end
c = C.new
c.examine_global #1
Trace how a global value remains accessible inside a method after class-definition scope changes. section 5.2.1
