support@90-10.dev

Ruby Global Variables

WARNING: this topic is purely "academic" - global variables should never be used. FYI, they produce warnings if the -w flag is in use by the interpreter.

Global variables' names start with the $ character and have a value of nil before first assignment.

$a_bad_idea = 1

class Animal
  def wooot?
    $a_bad_idea = 2
  end
end

blue_tiger = Animal.new

puts $a_bad_idea   # returns 1
blue_tiger.wooot?
puts $a_bad_idea   # returns 2

First time we print $a_bad_idea it shows 1. The second time is 2, although there is not immediately evident where the change occurred.