From: Adam Prescott Date: 2011-08-23T00:38:33+09:00 Subject: Re: New to Ruby some problems --20cf303f695c0c7c1504ab19df30 Content-Type: text/plain; charset=UTF-8 On Mon, Aug 22, 2011 at 3:05 PM, Chuck Remes wrote: > Sorry, I didn't read the problem closely enough (too early in the > morning!). You need to pass a ROW and COLUMN to set or get a value in your > matrix instead of just a single value like I posted above. > > Something like this might be a good start (untested). > > class Matrix > [...] > > def set(x, y, value) > @array[index(x, y)] = value > end > > def get(x, y) > @array[index(x,y)] > end > > [...] > end > Not sure if you know this, but you can actually still use the [] approach with multiple arguments, to avoid "set" and "get" method names. A stupid example: class X def []=(a, b, c) p [a, b] @a, @b = c, c.reverse end attr_reader :a, :b end x = X.new x[1, 2] = "foo" #=> [1, 2]; "foo" p x #=> # puts x.a #=> foo puts x.b #=> oof Live output: http://ideone.com/UxWNS --20cf303f695c0c7c1504ab19df30--