Advertisement
Zeriab

[RGSS1-3] Table pretty printer

Feb 4th, 2014
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.65 KB | None | 0 0
  1. class Table
  2.   ##
  3.   # Gives a special string representation. (For debugging purposes mostly)
  4.   #
  5.   def to_s
  6.     if ysize == 1
  7.       begin
  8.         return to_s_1d
  9.       rescue # In the rare case where you have a 2- or 3-dimensional
  10.       end    # table with ysize = 1
  11.     end
  12.     if zsize == 1
  13.       begin
  14.         return to_s_2d
  15.       rescue # In the rare case where you have a 3-dimensional table
  16.       end    # with zsize = 1
  17.     end
  18.     return to_s_3d
  19.   end
  20.  
  21.   ##
  22.   # Returns a representation of the 1-dimensional table as a string
  23.   # Assumes that the table is 1-dimensional. Will throw an error otherwise
  24.   #
  25.   def to_s_1d
  26.     str = '['
  27.     for i in 0...(xsize-1)
  28.       str += self[i].to_s + ', '
  29.     end
  30.     str += self[xsize-1].to_s + ']'
  31.     return str
  32.   end
  33.  
  34.   ##
  35.   # Returns a representation of the 2-dimensional table as a string
  36.   # Assumes that the table is 2-dimensional. Will throw an error otherwise
  37.   #
  38.   def to_s_2d
  39.     str = '['
  40.     for j in 0...ysize
  41.       str += "\n["
  42.       for i in 0...(xsize-1)
  43.         str += self[i,j].to_s + ', '
  44.       end
  45.       str += self[xsize-1,j].to_s + ']'
  46.     end
  47.     str += "\n]"
  48.     return str
  49.   end
  50.  
  51.   ##
  52.   # Returns a representation of the 3-dimensional table as a string
  53.   # Assumes that the table is 3-dimensional. Will throw an error otherwise
  54.   #
  55.   def to_s_3d
  56.     str = '{'
  57.     for k in 0...zsize
  58.       str += '['
  59.       for j in 0...ysize
  60.         str += "\n["
  61.         for i in 0...(xsize-1)
  62.           str += self[i,j,k].to_s + ', '
  63.         end
  64.         str += self[xsize-1,j,k].to_s + ']'
  65.       end
  66.       str += "\n]"
  67.     end
  68.     str += '}'
  69.     return str
  70.   end
  71. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement