Ruby Variable Scope

February 28, 2015

Ruby is an interesting language. One cool concept is that all variables mark their own scope via the first character in their name. Here are your options:

Global Variables

Global variables in Ruby start with the `$`sign.

$foo = 3 # Global 3
$BAR = 10 # Global 10

Constants

In Ruby, constants are variables that begin with capital letters.

A_CONST = 10 # 10
A_CONST = 20 # warning: already initialized constant A_CONST
FOO = 10 # This also works

Instance Variables

Ruby instance variables begin with the `@`sign.

class Person
  def initialize(name, age)
    @name = name
    @age = age
    birthplace = "San Francisco, CA"
  end
  
  def show
    puts @name
    puts @age
    puts birthplace
  end
end

Person.new("Jon",27).show # undefined local variable or method `birthplace'

Class Variables

Class variables in Ruby are very similar to instance variables. Instead of a single `@`sign, they begin with two, such as `@@foo`. While instance variables are shared by every method of an individual object, class variables are shared between all instances of a class.

class Person
  def initialize(name, age)
    @@name = name # Setting name will change it for all instances of Person
    @@age = age
    birthplace = "San Francisco, CA"
  end
  
  def show
    puts @@name
    puts @@age
    puts birthplace
  end
end

Person.new("Jon",27).show # undefined local variable or method `birthplace'