Q:

Undefined method << for NilClass

I am trying to write a transpose method but it just is not working out for me. The code is below:
def my_transpose(array)
  new_matrix= Array.new
  v_entries= 0
  h_entries= 0

while v_entries < array.length 
  while h_entries < array.length 
    new_matrix[h_entries] << ([array[h_entries][v_entries]])
    h_entries +=1
  end
  v_entries +=1
  h_entries= 0
end
new_matrix
end

array = [[1,2,3], [4,5,6], [7,8,9]]

I am trying to get my_transpose(array) to give me:
[[1,4,7], [2,5,8], [3,6,9]]

I tried adding 
new_matrix[h_entries] << ([array[h_entries][v_entries]]) 

but I get an error 

"undefined method `<<' for nil:NilClass"

Any insight would be great. 

A:

In your first loop iteration new_matrix[h_entries] would be nil. So, you can't use << method on it.
Try to initialize it to an empty array if it is nil:
new_matrix[h_entries] ||= []
new_matrix[h_entries] << ([array[h_entries][v_entries]])

