Local variables in an if statement

I wonder why it is displayed a?

if true
  puts 'true'
else
  puts 'false'
  a = 123
end

puts a # no error

# or 
# my_hash = {key: a}
# puts my_hash # :key => nil

But this causes an error, although "true" will be displayed

if true
  puts 'true'
else
  puts 'false'
  a = 123
end

puts a2 # boooooom
+5
source share
1 answer

The link ainside ifhas the effect of declaring it as a variable if there is no method for the object a=.

Ruby , , . , , . , .

+1

All Articles