From: Leslie Viljoen Date: 2005-08-13T00:57:52+09:00 Subject: Re: Array redefinition problem david@vallner.net wrote: >Cit�t Leslie Viljoen : > > > >>Hello! >> >>I have a large program which needs to be able to use Postgres OR MySQL. >>Since the MySQL module's query result has a num_rows method and Postgres >>returns an array, I thought I'd just redefine Array like this: >> >>class Array >> def num_rows >> return length >> end >>end >> >>But in another line (@entries = Array.new) I get: >> >>/var/www/ruby/core.rb:443:in `initialize': wrong number of arguments (0 >>for 2) (ArgumentError) >> >>Now if I do the same thing in a small test program, I get no errors. >>This works: >> >>class Array >> def num_rows >> return length >> end >>end >> >>res = Array.new >>puts res.num_rows >> >>In the large program I am not redefining Array anywhere else - what else >>can >>cause this error? >> >>Les >> >>-- >>ruby -e "puts 'Just another fickle programmer'" >> >>Leslie Viljoen [leslie@camary.co.za] >>Camary Consulting [http://www.camary.co.za] >>Cellphone [083-6186100] >>Personal web [http://mobeus.homelinux.org] >> >> >> >> >> >> > > >If the Array redefinition is nested in a module, you are shadowing the builtin >Array class with a locally defined one. Try: > > class ::Array > # Redefine methods. > end > >to make sure you're redefining the builtin one. > > It's a web script, so I never noticed this warning in a class below the array redef: warning: redefining Object#initialize may cause infinite loop I had a "begin" in there that was unmatched, and a rescue several functions later - which was causing some real strange things. Anyway, now that I have put the begin in the same function as the rescue, I no longer get the error. But I still can't redefine Array - now I get: /var/www/ruby/core.rb:22:in `num_rows': undefined local variable or method `length' for #<#::Array:0xb65e2c78> (NameError) The first lines of the program are: class ::Array def num_rows return length <--- LINE 22 end end a = Array.new puts a.num_rows -- so how do I access ::Array.length? -- ruby -e "puts 'Just another fickle programmer'" Leslie Viljoen [leslie@camary.co.za] Camary Consulting [http://www.camary.co.za] Cellphone [083-6186100] Personal web [http://mobeus.homelinux.org]