RichardOnRails
5/7/2008 2:34:00 PM
On May 6, 11:07 pm, Phrogz <phr...@mac.com> wrote:
> On May 6, 8:51 pm, RichardOnRails
>
> <RichardDummyMailbox58...@uscomputergurus.com> wrote:
> > Secondly, 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 - certainly not mine.
>
> > So I still wish someone would show where this visible undefined get
> > is, in fact, defined. In fact, it does something with the "splat"
> > operator ... and then I'm lost.
>
> Is this under vanilla Ruby or Rails? It simply doesn't work as you
> claim under Ruby alone:
>
> irb(main):001:0> class Matrix < Array; end
> => nil
> irb(main):002:0> m = Matrix.new
> => []
> irb(main):003:0> m.methods.grep /get/
> => ["instance_variable_get"]
> irb(main):004:0> m.get
> NoMethodError: undefined method `get' for []:Matrix
> from (irb):4
> from :0
Hi Phroz,
Thank you for responding.
> Is this under vanilla Ruby or Rails?
I think I've got "vanilla Ruby". In January '08, I wiped out my old
version of Ruby and used the ruby186-26_rc2.exe installer for
Windows. It came with Rails 2.0.2 or I installed Rails subsequently.
Albert suggested thst the "get" was never invoked, which I've
subsequently verified with the Ruby debugger. (I should have thought
to do that in the first place.) I put in trace statements to
demonstrate the real problem: "[]*args" returns a unitary array
containing the first subscript in each of my invocations.
If you're still dubious about it working as I indicated, below is my
debugging version and it's output.
Best wishes,
Richard
====================
Instrumented Program
====================
# TA.rb
# K:\_Projects\Ruby\_Ruby_Techniques\Sudoku\TA.rb
class Matrix < Array
def [] *args
puts "args = '#{args}', an #{args.class.to_s} object with length
#{args.length }"
if (args.length == 2) && args[0].is_a?(Integer) && args[1].is_a?
(Integer)
get(*args)
else
puts 'In "def [] *args", "else" clause'
super *args # raise IndexError? #
end
end
end
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]
=====================
Command Window output
=====================
K:\_Projects\Ruby\_Ruby_Techniques\Sudoku>ruby ta.rb
args = '0', an Array object with length 1
In "def [] *args", "else" clause
20
args = '2', an Array object with length 1
In "def [] *args", "else" clause
70
args = '1', an Array object with length 1
In "def [] *args", "else" clause
[40, 50, 60]
K:\_Projects\Ruby\_Ruby_Techniques\Sudoku>ta.rb
K:\_Projects\Ruby\_Ruby_Techniques\Sudoku>ruby ta.rb
args = '0', an Array object with length 1
In "def [] *args", "else" clause
20
args = '2', an Array object with length 1
In "def [] *args", "else" clause
70
args = '1', an Array object with length 1
In "def [] *args", "else" clause
[40, 50, 60]
K:\_Projects\Ruby\_Ruby_Techniques\Sudoku>