From: RichardOnRails Date: 2008-05-08T01:45:03+09:00 Subject: Re: get method in Array subclass: where's it defined? On May 7, 4:06 am, Albert Schlef wrote: > RichardOnRails wrote: > > m = Matrix[ [10,20,30], [40,50,60], [70,80,90] ]; > > puts m [0] [1].inspect # 20 > > puts m [2] [0].inspect # 70 > > puts m[1].inspect # [40, 50, 60] > [...] > > my adaptation of Daniel's suggestion, including sub- > > classing, works with no definition of get anywhere. And the code runs > > without any visible definition of get > > Your code "works with no definition of get anywhere" because it doesn't > utilize this 'get'. When you do "puts m [0] [1]" you aren't triggering > your 'get'. That's because "m [0] [1]" is the plain-vanila array > indexing; it uses one-dimensional indexing. It's exactly like doing "row > = m[0]; row[1]". Multi-dimentional array indexing means: "m[0,1]". and > if you tried that you'd trigger the call to 'get', and since it's > absence, Ruby would complain. > > Implementiong your 'get' is no big deal. For a start, replace that > "get(*args)" with "self[args[0]][args[1]]". > -- > Posted viahttp://www.ruby-forum.com/. Hi Albert, As you obviously recognized that I was totally out at sea on this thing. The instrumented version, which I added in my reply to Phroz, confirms your assessment that "get" was never invoked. While I'm grateful for that revelation, I am ever more grateful for your additional, thorough analysis of the situation. I will employ them to good effect. What's really at the root of my problems with this thing is my ignorance about the def [] * args. It appears that what's being defined is "args". I concluded this by changing its name and drawing an "undefined local variable or method `args'" error message for the next line. To me, that seems weird. It looks like C or C++ with type info before the name of a variable being defined. I can't find "def [] *" on the net. Can you shed any light on this? Best wishes, Richard