Skip to content

Instantly share code, notes, and snippets.

Created August 13, 2017 20:33
Show Gist options
  • Save anonymous/1b20b0e7a2e84608c5b7746dfd7b6ebd to your computer and use it in GitHub Desktop.
Save anonymous/1b20b0e7a2e84608c5b7746dfd7b6ebd to your computer and use it in GitHub Desktop.
Ruby subarrays
class FdArray
def initialize(totalSize)
@data_array = Array.new(totalSize){[]}
end
def addOn(location,data)
@data_array[location].push(data)
end
def write(location, data)
@data_array[location] = data
end
def read(location)
return @data_array[location]
end
def giveAll
return @data_array
end
end
#---------------------------------------------
# TEST NORMAL Array
#---------------------------------------------
testArray = [[],[]]
#These should put two items in the array (sub-array) in position 0
testArray[0]<<"test1"
testArray[0].push("test2")
print("0,0:",testArray[0][0])
print("0,1:",testArray[0][1])
print("\n\n TEST ARR :",testArray)
#Direct write
testArray[1][1]="x"
print("\n\n TEST ARR :",testArray)
#---------------------------------------------
# TEST CLASS ARRAY
#---------------------------------------------
fd = FdArray.new(10)
#This should 'push' one item data onto the subarray
fd.addOn(4,10)
fd.read(4)
print("\n\n FD ARRAY: \n",fd.giveAll)
#Test with a direct write
fd2 = FdArray.new(10)
fd.write2(4,1,10)
print("\n\n FD ARRAY: \n",fd2.giveAll)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment