Ruby Language Local Variables


Example

Local variables (unlike the other variable classes) do not have any prefix

local_variable = "local"
p local_variable
# => local

Its scope is dependent on where it has been declared, it can not be used outside the "declaration containers" scope. For example, if a local variable is declared in a method, it can only be used inside that method.

def some_method
    method_scope_var = "hi there"
    p method_scope_var
end

some_method
# hi there
# => hi there

method_scope_var
# NameError: undefined local variable or method `method_scope_var'

Of course, local variables are not limited to methods, as a rule of thumb you could say that, as soon as you declare a variable inside a do ... end block or wrapped in curly braces {} it will be local and scoped to the block it has been declared in.

2.times do |n|
    local_var = n + 1
    p local_var
end
# 1
# 2
# => 2

local_var
# NameError: undefined local variable or method `local_var'

However, local variables declared in if or case blocks can be used in the parent-scope:

if true
    usable = "yay"
end

p usable
# yay
# => "yay"

While local variables can not be used outside of its block of declaration, it will be passed down to blocks:

my_variable = "foo"

my_variable.split("").each_with_index do |char, i|
    puts "The character in string '#{my_variable}' at index #{i} is #{char}"
end
# The character in string 'foo' at index 0 is f
# The character in string 'foo' at index 1 is o
# The character in string 'foo' at index 2 is o
# => ["f", "o", "o"]

But not to method / class / module definitions

my_variable = "foo"

def some_method
    puts "you can't use the local variable in here, see? #{my_variable}"
end

some_method
# NameError: undefined local variable or method `my_variable'

The variables used for block arguments are (of course) local to the block, but will overshadow previously defined variables, without overwriting them.

overshadowed = "sunlight"

["darkness"].each do |overshadowed|
    p overshadowed
end
# darkness
# => ["darkness"]

p overshadowed
# "sunlight"
# => "sunlight"