From: Robert Klemme Date: 2010-01-24T20:50:23+09:00 Subject: Re: how to define []= On 01/24/2010 05:03 AM, Colin Bartlett wrote: > 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! Adding to that: there is a significant drawback to doing [][]: you need to return an object from the first [] in order to be able to invoke the second []. Depending on the internal implementation of the class this might be easy (i.e. if you have that object, say a nested Array, anyway) or it might impose some overhead (i.e. if the internal structure is different and you need to return a proxy object for the sole purpose of receiving the second []. >> First of all, I don't think that will work. > > Well, things like that worked a year or so ago, It will certainly work as Colin demonstrated. Since the advent of 1.9 we have extended pattern matching for block and method arguments, so we can do things like this: irb(main):001:0> class X irb(main):002:1> def []=(*a,b) irb(main):003:2> printf "a=%p b=%p\n",a,b irb(main):004:2> end irb(main):005:1> end => nil irb(main):006:0> o = X.new => # irb(main):007:0> o[1,2,3]=567 a=[1, 2, 3] b=567 => 567 irb(main):008:0> o[1,2,3]=567,234 a=[1, 2, 3] b=[567, 234] => [567, 234] irb(main):009:0> o[1]=2 a=[1] b=2 => 2 irb(main):010:0> o[1,2]=3 a=[1, 2] b=3 => 3 irb(main):011:0> In other words: it's easier to analyze argument lists. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/