Skip to content

Instantly share code, notes, and snippets.

/ruby.rb Secret

Created March 15, 2016 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/38748b1c6831dad868be to your computer and use it in GitHub Desktop.
Save anonymous/38748b1c6831dad868be to your computer and use it in GitHub Desktop.
@matrix = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
## with the below code, only the first zero in the whole matrix is changed
@matrix[0][0] = 1
p @matrix
## 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
@matrixsize = 10
@blankrow = []
until @blankrow.length == @matrixsize
@blankrow << 0
end
@matrix = []
until @matrix.length == @matrixsize
@matrix << @blankrow
end
## 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
@matrix[0][0] = 1
p @matrix
## 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment