Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Table
- ##
- # Gives a special string representation. (For debugging purposes mostly)
- #
- def to_s
- if ysize == 1
- begin
- return to_s_1d
- rescue # In the rare case where you have a 2- or 3-dimensional
- end # table with ysize = 1
- end
- if zsize == 1
- begin
- return to_s_2d
- rescue # In the rare case where you have a 3-dimensional table
- end # with zsize = 1
- end
- return to_s_3d
- end
- ##
- # Returns a representation of the 1-dimensional table as a string
- # Assumes that the table is 1-dimensional. Will throw an error otherwise
- #
- def to_s_1d
- str = '['
- for i in 0...(xsize-1)
- str += self[i].to_s + ', '
- end
- str += self[xsize-1].to_s + ']'
- return str
- end
- ##
- # Returns a representation of the 2-dimensional table as a string
- # Assumes that the table is 2-dimensional. Will throw an error otherwise
- #
- def to_s_2d
- str = '['
- for j in 0...ysize
- str += "\n["
- for i in 0...(xsize-1)
- str += self[i,j].to_s + ', '
- end
- str += self[xsize-1,j].to_s + ']'
- end
- str += "\n]"
- return str
- end
- ##
- # Returns a representation of the 3-dimensional table as a string
- # Assumes that the table is 3-dimensional. Will throw an error otherwise
- #
- def to_s_3d
- str = '{'
- for k in 0...zsize
- str += '['
- for j in 0...ysize
- str += "\n["
- for i in 0...(xsize-1)
- str += self[i,j,k].to_s + ', '
- end
- str += self[xsize-1,j,k].to_s + ']'
- end
- str += "\n]"
- end
- str += '}'
- return str
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement