From: Colin Bartlett Date: 2010-01-24T13:03:08+09:00 Subject: Re: how to define []= On Sat, Jan 23, 2010 at 4:56 PM, Marnen Laibow-Koser wrote: > Second, it's a bad idea: to experienced Ruby programmers, > [i, j] is slicing, not subscripting.  Just use the standard [i][j]. You're right to observe that it's likely to be confused with slice, a point which had escaped me when I made my post: arr = [0, 1, 2, 3, 4] #=> [0, 1, 2, 3, 4] arr[1, 3] = 7 #=> 7 arr #=> [0, 7, 4] So, yes, the sort of thing in my post should almost always be avoided. That said, there may be occasions when it might be useful, suitably documented of course! > First of all, I don't think that will work. Well, things like that worked a year or so ago, when I was looking closely into how Ruby assignment worked with []= methods, and they still seem to work! I think the following shows how Ruby deals with a mix of arguments on the LHS and RHS of an assignment method which uses []= class QQ def []=( *args ) puts "#args== " + args.inspect # you can put some complicated code here # which relies on the structure of args end end q = QQ.new #=> # q[] = 100 #args== [100] #=> 100 q[0] = 100 #args== [0, 100] #=> 100 q[0, 1] = 100 #args== [0, 1, 100] #=> 100 q[0, 1, 2] = 100 #args== [0, 1, 2, 100] #=> 100 q[] = 100, 101 #args== [[100, 101]] #=> [100, 101] q[0] = 100, 101 #args== [0, [100, 101]] #=> [100, 101] q[0, 1] = 100, 101 #args== [0, 1, [100, 101]] #=> [100, 101] q[0, 1, 2] = 100, 101 #args== [0, 1, 2, [100, 101]] #=> [100, 101] You can even do things like: q[0, [10, 11], 2] = 100, 101 #args== [0, [10, 11], 2, [100, 101]] #=> [100, 101] But I'll end by repeating my endorsement of your admonishment!