On Mon, Aug 22, 2011 at 11:26 PM, jack jones <shehio_22 / hotmail.com> wrote: > I am sorry I can't fix the errors though :S:S! > > Attachments: > http://www.ruby-forum.com/attachment/6538/2Dto1D.rb when in ruby, think in ruby. when new in language, start slowly, a line of code at a time, rtfm. try eg, $ cat -n test_matrix.rb 1 # This is a program to store a two dimension array into one dimension only .. ROW major 2 class MATRIX 3 attr_reader :array 4 def initialize(rows, cols) 5 @cols = cols 6 @array = Array.new(rows*cols){ rand(10)} 7 end 8 9 # set desired row and column and value 10 def []=(row, column, value) 11 position = index row, column 12 @array[position] = value 13 end 14 15 # retrieve value from row, column 16 def [](row, column) 17 position = index row, column 18 return @array[position] 19 end 20 21 private 22 def index row, column 23 return (row-1) * @cols + (column-1) # ruby array index starts at 0 24 end 25 end 26 27 28 mat = MATRIX.new(2,2) 29 puts "before" 30 p mat.array 31 32 puts "after" 33 mat[1,1]=11 34 mat[1,2]=12 35 mat[2,1]=21 36 mat[2,2]=22 37 38 puts mat[1,1] 39 puts mat[1,2] 40 puts mat[2,1] 41 puts mat[2,2] 42 43 p mat.array 44 45 $ ruby test_matrix.rb before [5, 8, 6, 4] after 11 12 21 22 [11, 12, 21, 22] kind regards -botp