From: Robert Klemme Date: 2008-12-03T19:20:06+09:00 Subject: Re: Question about op. overloading 2008/12/3 Maarten Mortier : > What about > sudoku[r][c]= a > though? > > Is that possible? I thought I'd be able to fluke it by overloading [] to > return an array. But how do I pass parameters inside the bracket? You do not need an array but rather a proxy: class X ColProxy = Struct.new :owner, :column do def [](row) owner.matrix[[column, row]] end def []=(row,val) owner.matrix[[column, row]] = val end end attr_reader :matrix def initialize @matrix = {} end def [](column) ColProxy.new(self, column) end end irb(main):026:0> x = X.new => # irb(main):027:0> x[1][2] = 3 => 3 irb(main):028:0> x[1][2] => 3 irb(main):029:0> x => #3}> > And how woudl I get > > puts sudoku[][3] > > to return the third column? You can do it similarly. Just make the argument for X#[] optional and return another proxy. I would really rather go with the other approach to stuff all coordinates in a single pair of brackets and or provide additional methods. Kind regards robert -- remember.guy do |as, often| as.you_can - without end