Variables in loops

Take a look at the silly example:

2.times do |i|
  x = uninitialized Int32
  if x == 50
    puts "x = 50, i = #{i}"
    puts " (by chance!)" if i == 0
  end
  x = 50
end

Is x created anew on each iteration, and by chance occupies the same position in memory than the x of the previous iteration (and that’s why it has the value 50 the second time)?

Or it’s the same variable in both iterations and I can count on it?

uninitialized means the variable uses a memory location with whatever data that memory holds at that time. That’s the point of uninitialized: the memory is not cleared or initialized with a default value.

In this example, the variable x happens to occupy the same memory location in each loop. Even though you can probably always observe the behaviour due to how the stack works, you should not rely on it. It is not the same variable carried over between iterations.
The compiler / code generator is free to move things around if it feels like it.