From: RichardOnRails Date: 2008-05-08T22:00:03+09:00 Subject: Re: get method in Array subclass: where's it defined? On May 7, 12:41 pm, RichardOnRails wrote: > 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 Hello, again, Daniel and Albert, You've both been very gracious and generous in educating me in the Ruby Way (to borrow Hal Fulton's apt title). So far I've only written "toy" Ruby apps, but on those occasions I strive to write the best Ruby I can. Too often, that effort becomes insoluble solely with my books and web searches. Your wonderful tutorials have elevated my Ruby skills, for which I am very grateful. I now understand all the nuances that had troubled me. I hope others stumble across this thread and benefit from it as I have. Thanks and Best wishes, Richard Muller