I am trying to design a small program to print the third greatest number from a provided array of numbers. I thought I had designed it to assign the largest of two numbers to a global variable called ‘the_greatest’, and other to one called ‘second_greatest’, or to a third called ‘the_third_greatest’, then another iteration of the while loop would compare again and find the appropriate numbers. However, I ran the method and it is printing out 0’s(the default values for the variables before the while loop). My only conclusion is that the variables in the while loop cannot change the values of the same variables outside of the while loop. If this is the case, why?

Here is my code: Thanks!

```
def third_greatest(nums)
idx = 0
idx2 = idx + 1
the_greatest = 0
second_greatest = 0
the_third_greatest = 0
while idx < nums.length
if nums[idx] > nums[idx2]
greatest = nums[idx]
if greatest > the_greatest
the_greatest = greatest
elsif
greatest > second_greatest
second_greatest = greatest
elsif
greatest > the_third_greatest
the_third_greatest = greatest
end
idx += 1
else
greatest = nums[idx2]
if greatest > the_greatest
the_greatest = greatest
elsif
greatest > second_greatest
second_greatest = greatest
elsif
greatest > the_third_greatest
the_third_greatest = greatest
end
idx += 1
end
end
puts the_third_greatest
end
puts third_greatest([1,2,3,4])
```