Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
44 lines (38 sloc) 1.01 KB
layout title jumbo
All the Variables of Ruby

In ruby there are six different types of variables. Some very obscure and rarely used, but interesting to know.

Local Variable

Used in the scope of a method or other local usage.

name = 'stuart'


Usually used as a value that is set once to set a default or reusable value. They use all caps as a convention.

class Person
  TYPES = [:admin, :user].freeze
  # hint use the freeze method to make constants immutable

Instance Variable

Saves a values to the instance of a class.

def name=(name)
  @name = name

Class Variable

Saves a value to the class itself. Works almost like a class constant.

def self.count_plus_one
  @@count += 1

Global Variable

A variable that can be accessed anywhere from within the program. Admittedly I have never seen these used. Perhaps it makes more sense to make a class, nevertheless it's worth mentioning.

$access_me_anywhere = 42