accessing values in two dimensional arrays
Posted
by
BrainLikeADullPencil
on Stack Overflow
See other posts from Stack Overflow
or by BrainLikeADullPencil
Published on 2012-11-18T04:47:14Z
Indexed on
2012/11/18
5:00 UTC
Read the original article
Hit count: 165
ruby
In some code I'm trying to learn from, the Maze string below is turned into an array (code not shown for that) and saved in the instance variable @maze. The starting point of the Maze is represented by the letter 'A' in that Maze, which can be accessed at @maze[1][13]
---row 1, column 13. However, the code I'm looking at uses @maze[1][13,1] to get the A, which you can see returns the same result in my console. If I do @maze[1][13,2], it returns the letter "A " with two blank spaces next to it, and so on. [13,3] returns "A " with three blank spaces.
Does the 2 in [13,2] mean, "return two values starting at [1][13]? If so, why? Is this some feature of arrays or two dimensional arrays that I don't get?
[20] pry(#<Maze>):1> @maze[1][13]
=> "A"
[17] pry(#<Maze>):1> @maze[1][13,1]
=> "A"
[18] pry(#<Maze>):1> @maze[1][13,2]
=> "A "
[19] pry(#<Maze>):1> @maze[1][13,3]
=> "A "
Maze String
MAZE1 = %{#####################################
# # # #A # # #
# # # # # # ####### # ### # ####### #
# # # # # # # # #
# ##### # ################# # #######
# # # # # # # # #
##### ##### ### ### # ### # # # # # #
# # # # # # B# # # # # #
# # ##### ##### # # ### # # ####### #
# # # # # # # # # # # #
# ### ### # # # # ##### # # # ##### #
# # # # # # #
#####################################}
© Stack Overflow or respective owner