3. Local Variable
• Syntax: all lowercase and underscores
• Example: prisoner_1
• Key Idea: Stuck inside a method
4. Prisoners can’t move to different
rooms on their own.
San Quentin = Prison.new
def wardens_office
def cell_1
warden= prisoner_1
prisoner_1 = martha
end
end
def cell_2
prisoner_2 = prisoner_1
end
5. Class Prison
def cell_1
prisoner1 = martha
prisoner2 = prisoner1
end
def warden_office
‘doesn’t know anything about ‘prisoner1’
end
end
6. Instance Variable
• Syntax: starts with ‘@’
• Example: @warden
• Key Idea: Can be used across all methods in a
class instance
7. Warden can move to any room in HIS
prison. When he is changed it affects
all methods in THIS prison
San Quentin = Prison.new
@warden = def wardens_office_1
end warden= prisoner_1
def cell_1 Other_variable = @warden
prisoner_1 = martha end
new_variable = @warden
end
def cell_2
prisoner_2 = prisoner_1
new_variable = @warden
end
8. Class Prison
attr_accessor :warden
def initialize
@warden
end
def cell_1
new_variable = @warden
end
def warden_office
other_variable = @warden
end
end
9. Class Variable
• Syntax: starts with ‘@@’
• Example: @@Commisioner_of_prisons
• Key Idea:
– Can be changed by any instance,
– It is changed for ALL instances of the class
10. Warden can move to any room in HIS
prison. When he is changed it affects
all methods in THIS prison
San Quentin = Prison.new Rickers_Island = Prison.new
@@commisioner = pipe_man @@commisioner = pipe_man
linked
end end
cell_1
cell_2
11. Global Variable
• Syntax: starts with ‘$’
• Example: $president
• Key idea: Can go anywhere in your program.
When changed it affects every place it is used.
– Any class or method.