Ruby 3D Array implementation
# beautiful implementation of a ruby 3D array
# although I am a little irritated you can't just do it the java way and slap
# an extra pair of braces on the end and get a whole extra dimension
class Array3
def initialize
@store = [[[]]]
end
def [](a,b,c)
if @store[a]==nil ||
@store[a][b]==nil ||
@store[a][b][c]==nil
return nil
else
return @store[a][b][c]
end
end
def []=(a,b,c,x)
@store[a] = [[]] if @store[a]==nil
@store[a][b] = [] if @store[a][b]==nil
@store[a][b][c] = x
end
end
x = Array3.new
x[0,0,0] = 5
x[0,0,1] = 6
x[1,2,3] = 99
puts x[1,2,3]
# from http://www.samspublishing.com/
# although I am a little irritated you can't just do it the java way and slap
# an extra pair of braces on the end and get a whole extra dimension
class Array3
def initialize
@store = [[[]]]
end
def [](a,b,c)
if @store[a]==nil ||
@store[a][b]==nil ||
@store[a][b][c]==nil
return nil
else
return @store[a][b][c]
end
end
def []=(a,b,c,x)
@store[a] = [[]] if @store[a]==nil
@store[a][b] = [] if @store[a][b]==nil
@store[a][b][c] = x
end
end
x = Array3.new
x[0,0,0] = 5
x[0,0,1] = 6
x[1,2,3] = 99
puts x[1,2,3]
# from http://www.samspublishing.com/
Labels: array, nerd, programming, Ruby
0 Comments:
Post a Comment
<< Home