Ruby Ruby Loops Ruby Iteration Iteration With Each

does ruby ignore updates to index because its not in the why condition? is index updated and not referenced?

def arr_writer(arr)
  i = 0
  item = arr[i]
  while i < arr.length
    puts "The current item is #{item} for index #{i}"
    i+=1
  end
end
```ruby 

a simple fix is to update the item variable inside the while loop but this seems wrong? 

```ruby 
def arr_writer(arr)
  i = 0
  item = arr[i]
  while i < arr.length
    puts "The current item is #{item} for index #{i}"
    i+=1
    item =arr[i]
  end
end
```ruby

1 Answer

KRIS NIKOLAISEN
KRIS NIKOLAISEN
54,196 Points

item takes the value at the time it is assigned. Without putting the assignment in the loop item will always be arr[0].