← all posts

22 October 2008

Ruby Quiz - Matricies

nb = 5
distance = Array.new(nb, Array.new(nb))
k = 0
(0...nb).each do |i|
  (0...nb).each do |j|
    distance[j][i] = k +=1
  end
end

puts distance.inspect

This code should generate: [[1,6,11,16,21], [2,7,12,17,22], [3,8,13,18,23], [4,9,13,19,25], [5,10,15,20,25]]

But generates: [[5,10,15,20,25], [5,10,15,20,25], [5,10,15,20,25], [5,10,15,20,25], [5,10,15,20,25]]

I learned why today, and I wonder if anyone else knows.

— Ryan

Got a thought? Email me or find me on Mastodon.

Before this: Did you (Rails) Rumble?

After this: This is genius!