From: Marnen Laibow-Koser Date: 2010-01-24T01:56:34+09:00 Subject: Re: how to define []= Colin Bartlett wrote: > On Sat, Jan 23, 2010 at 2:29 PM, hagbard > wrote: >>> But how do we define the set method `[]='? >> >> Just define it. >> def []=(i, value) >> � @ary[i] = value >> end > > And for a two dimensional array: > class QQ > def initialize( arr ) ; @ary = arr ; end > def []( i, j ) ; @ary[i][j] ; end > def []=( i, j, value ) ; @ary[i][j] = value ; end > end First of all, I don't think that will work. Second, it's a bad idea: to experienced Ruby programmers, [i, j] is slicing, not subscripting. Just use the standard [i][j]. > > a = [ [0, 1], [10], [20, 21, 22] ] > q = QQ.new( a ) > puts q[2, 1] > q[2, 1] = 2211 > puts q[2, 1] Best, -- Marnen Laibow-Koser http://www.marnen.org marnen@marnen.org -- Posted via http://www.ruby-forum.com/.