Local Variables in Ruby

Viewed 49

Why does ruby define a local variable for a, even if the code block does never run?

class Something
  attr_reader :a

  def call
    puts "#{defined?(a)}"
    if false
      a = 1
    end
    puts "#{defined?(a)}"
  end
end

Something.new.call
# method
# local-variable

I would expect a to be a method throughout call. It seems that ruby detects a potentially assignment and creates the local var. Why?

0 Answers
Related