Advertisement
Guest User

Untitled

a guest
Mar 15th, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. @matrix = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  2. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  3. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  4. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  5. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  6. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  7. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  8. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  9. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  10. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
  11.  
  12.  
  13. ## with the below code, only the first zero in the whole matrix is changed
  14.  
  15. @matrix[0][0] = 1
  16. p @matrix
  17.  
  18.  
  19. ## the below code resets the matrix, but in a purely implicit way which allows the matrix's size to be set in just one variable
  20.  
  21. @matrixsize = 10
  22.  
  23. @blankrow = []
  24. until @blankrow.length == @matrixsize
  25. @blankrow << 0
  26. end
  27.  
  28. @matrix = []
  29. until @matrix.length == @matrixsize
  30. @matrix << @blankrow
  31. end
  32.  
  33. ## however, this method of resetting the matrix produces a weird result that doesn't make sense when you try to change only the first zero again
  34.  
  35. @matrix[0][0] = 1
  36. p @matrix
  37.  
  38. ## please note i tried running this code on https://repl.it which runs ruby 2.2 from 2014-12 and it produces the same result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement