M
matt neuburg
Here's a simple variable initialization / scope question. In the
following code:
h = {:test => "cool"}
k = :test
result = nil
puts "#{result}" if (result = h[k])
....if I omit the third line (initializing "result"), I get an error.
Why? Intuitively I would have expected the second part of the 4th line
to create / initialize "result".
Another way of understanding my confusion is to notice that this *does*
work:
h = {:test => "cool"}
k = :test
if (result = h[k]) then puts "#{result}" end
So evidently I'm asking about the difference between the last line of
the first example and the last line of the second example. Intuitively I
would have expected these lines to be absolutely equivalent, but clearly
they are not. I'd like to understand the difference rigorously. Thx! m.
following code:
h = {:test => "cool"}
k = :test
result = nil
puts "#{result}" if (result = h[k])
....if I omit the third line (initializing "result"), I get an error.
Why? Intuitively I would have expected the second part of the 4th line
to create / initialize "result".
Another way of understanding my confusion is to notice that this *does*
work:
h = {:test => "cool"}
k = :test
if (result = h[k]) then puts "#{result}" end
So evidently I'm asking about the difference between the last line of
the first example and the last line of the second example. Intuitively I
would have expected these lines to be absolutely equivalent, but clearly
they are not. I'd like to understand the difference rigorously. Thx! m.