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'"
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?
My guess is that it's unrelated to your change of the Array class. Did
you check line 443 of core.rb?
Why dont you use one of the db wrappers, like dbi ?
Have you seen active_record ?
···
On 8/12/05, Leslie Viljoen <leslie@camary.co.za> wrote:
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'"
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'"
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?
My guess is that it's unrelated to your change of the Array class. Did
you check line 443 of core.rb?
robert
line 443:
@entries = Array.new
When I take Array redefinition change out, the whole program runs fine, except
that it cannot call Array.num_rows, which is what I want.
···
--
ruby -e "puts 'Just another fickle programmer'"
Why dont you use one of the db wrappers, like dbi ?
Have you seen active_record ?
Hm, maybe a good idea. Although I do actually want to
solve this problem. Quite often with Ruby I get strange
errors and end up spending a lot of time commenting
large blocks of code out to try and narrow them dDoes anyone know of a
program that can underown.
My pet peeve is the "syntax error" on the last line of
code - meaning I have missed an "end" somewhere.
···
On 8/12/05, Leslie Viljoen <leslie@camary.co.za> wrote:
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'"
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'"
When I take Array redefinition change out, the whole program runs fine, except
that it cannot call Array.num_rows, which is what I want.
I wonder whether there's another Array class, inside a module
namespace, or something like that. I can't really puzzle it out, but
the problem could, I think, lie in that direction. Or in some kind of
name clash introduced by a library you're require'ing, maybe.
My pet peeve is the "syntax error" on the last line of
code - meaning I have missed an "end" somewhere.
Religious indentation is your best friend, putting a comment next to an "end" to
signify what you're ending is another. Then have Emacs reindent the whole file
and finding the stray "end" shouldn't be difficult
/var/www/ruby/core.rb:22:in `num_rows': undefined local variable or
method `length' for #<#<Module:0xb65fcfec>::Array:0xb65e2c78> (NameError)
Ah wait, it seems whatever is running the web script is doing some voodoo with
namespaces - the global namespace isn't what it's supposed to be, in which case
not even the aliasing would work properly.
I'm possibly summoning the Daemon Obfuscatis here, but this should work no
matter what:
> 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 #<#<Module:0xb65fcfec>::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?
Yup, alias does indeed seem to be a better solution here. There might be a
slight bug with namespaces of methods implemented in C or something. Try using
explicitly self.length, if only for academic purposes?