Ruby – Variable Scope

previously we saw that any variables set in a method will be confined to that method’s scope. Also a method can’t access any variables outside it’s scope. e.g.:

website_name = "codingbee"
puts website_name

def normal_welcome
  # puts website_name           # this will output an error message, hence commented out.  
  website_name = "google"
  puts website_name
end

normal_welcome

puts website_name

This outputs:

PS C:Tempirb> ruby .scope.rb
codingbee
google
codingbee
PS C:Tempirb>

However this rule doesn’t apply to lower level blocks such as:

  • if-else blocks
  • loop blocks
  • begin-end blocks

Here’s some examples:

number = 5

if number < 10
  a = 20
  puts a
end

puts a

begin 
   b = 25
   puts b
end

puts b

c = 1
while c < 5
  c +=1
  puts c
end

puts c

This outputs:

PS C:\Temp\irb> ruby .\scope.rb
20
20
25
25
2
3
4
5
5
PS C:\Temp\irb>

However blocks are an exception to this and they do introduce scope:

1.upto(10) { |i|
  a = i
  puts i
  puts a
}
  
# puts i    # error: undefined variable. 
# puts a    # error: undefined variable.    

this outputs:

PS C:\Temp\irb> ruby .\blockscope.rb
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
PS C:\Temp\irb>