0

In Ruby (v2.5.0)...

[1,2,3].map do |i|
  if i.eql?(3)
    a = 123
  end
  defined?(a)
end
=> ["local-variable", "local-variable", "local-variable"]

Can someone please explain to me how a can be a local-variable (equal to nil) in the first and second iteration, if it's not set until the third iteration?

Thanks in advance!

chattsm
  • 4,651
  • 5
  • 19
  • 20
  • 4
    This question can be simplified to `if false; a = 42; end; defined?(a)`. – ndnenkov Jan 26 '18 at 15:15
  • 2
    See [Local Variables and Methods](http://ruby-doc.org/core-2.5.0/doc/syntax/assignment_rdoc.html#label-Local+Variables+and+Methods): _"The local variable is created when the parser encounters the assignment, not when the assignment occurs"_ – Stefan Jan 26 '18 at 16:01

1 Answers1

2

I will answer quoting a book by A.Black: Well Grounded Rubyist, Chapter 6, p. 158. (second edition 2014):

When the Ruby parser sees the sequence identifier, equal-sign, and value, as in this expression,

a = 123

it allocates space for a local variable a. The creation of the variable - not the assignment of a value to it, but the internal creation of a variable - always takes place as a result of this kind of expression, event if the code isn't executed.

maicher
  • 2,625
  • 2
  • 16
  • 27