Array redefinition problem

L

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 [[email protected]]
Camary Consulting [http://www.camary.co.za]
Cellphone [083-6186100]
Personal web [http://mobeus.homelinux.org]
 
R

Robert Klemme

Leslie said:
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?

My guess is that it's unrelated to your change of the Array class. Did
you check line 443 of core.rb?

robert
 
R

Reyn Vlietstra

heh, daars min afrikaaners op ruby-talk :)

Why dont you use one of the db wrappers, like dbi ?
Have you seen active_record ?

Hello!
=20
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:
=20
class Array
def num_rows
return length
end
end
=20
But in another line (@entries =3D Array.new) I get:
=20
/var/www/ruby/core.rb:443:in `initialize': wrong number of arguments (0
for 2) (ArgumentError)
=20
Now if I do the same thing in a small test program, I get no errors.
This works:
=20
class Array
def num_rows
return length
end
end
=20
res =3D Array.new
puts res.num_rows
=20
In the large program I am not redefining Array anywhere else - what else
can
cause this error?
=20
Les
=20
--
ruby -e "puts 'Just another fickle programmer'"
=20
Leslie Viljoen [[email protected]]
Camary Consulting [http://www.camary.co.za]
Cellphone [083-6186100]
Personal web [http://mobeus.homelinux.org]
=20
=20
=20
=20


--=20
Reyn Vlietstra
 
L

Leslie Viljoen

Robert said:
Leslie Viljoen wrote:



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'"

Leslie Viljoen [[email protected]]
Camary Consulting [http://www.camary.co.za]
Cellphone [083-6186100]
Personal web [http://mobeus.homelinux.org]
 
L

Leslie Viljoen

Reyn said:
heh, daars min afrikaaners op ruby-talk :)
heh. van waaraf is jy?
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.
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 [[email protected]]
Camary Consulting [http://www.camary.co.za]
Cellphone [083-6186100]
Personal web [http://mobeus.homelinux.org]


--
ruby -e "puts 'Just another fickle programmer'"

Leslie Viljoen [[email protected]]
Camary Consulting [http://www.camary.co.za]
Cellphone [083-6186100]
Personal web [http://mobeus.homelinux.org]
 
D

David A. Black

Hi --

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.

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.


David
 
D

david

Cit=E1t Leslie Viljoen said:
Hello!
=20
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 Postgre= s
returns an array, I thought I'd just redefine Array like this:
=20
class Array
def num_rows
return length
end
end
=20
But in another line (@entries =3D Array.new) I get:
=20
/var/www/ruby/core.rb:443:in `initialize': wrong number of arguments (0
for 2) (ArgumentError)
=20
Now if I do the same thing in a small test program, I get no errors.
This works:
=20
class Array
def num_rows
return length
end
end
=20
res =3D Array.new
puts res.num_rows
=20
In the large program I am not redefining Array anywhere else - what els= e
can
cause this error?
=20
Les
=20
--=20
ruby -e "puts 'Just another fickle programmer'"
=20
Leslie Viljoen [[email protected]]
Camary Consulting [http://www.camary.co.za]
Cellphone [083-6186100]
Personal web [http://mobeus.homelinux.org]
=20
=20
=20
=20


If the Array redefinition is nested in a module, you are shadowing the bu=
iltin
Array class with a locally defined one. Try:

class ::Array
# Redefine methods.
end

to make sure you're redefining the builtin one.
 
D

david

Cit=E1t Leslie Viljoen said:
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 ;)

David Vallner
 
L

Leslie Viljoen

Cit=E1t Leslie Viljoen <[email protected]>:

=20
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 Postgre= s
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 =3D 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 =3D Array.new
puts res.num_rows

In the large program I am not redefining Array anywhere else - what els= e
can
cause this error?

Les

--=20
ruby -e "puts 'Just another fickle programmer'"

Leslie Viljoen [[email protected]]
Camary Consulting [http://www.camary.co.za]
Cellphone [083-6186100]
Personal web [http://mobeus.homelinux.org]




=20


If the Array redefinition is nested in a module, you are shadowing the b= uiltin
Array class with a locally defined one. Try:

class ::Array
# Redefine methods.
end

to make sure you're redefining the builtin one.
=20

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 =3D Array.new
puts a.num_rows =20

-- so how do I access ::Array.length?

--=20
ruby -e "puts 'Just another fickle programmer'"

Leslie Viljoen [[email protected]]
Camary Consulting [http://www.camary.co.za]
Cellphone [083-6186100]
Personal web [http://mobeus.homelinux.org]
 
B

Brian Schröder

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
=20
I had a "begin" in there that was unmatched, and a rescue several
functions later -
which was causing some real strange things.
=20
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:
=20
/var/www/ruby/core.rb:22:in `num_rows': undefined local variable or
method `length' for #<#<Module:0xb65fcfec>::Array:0xb65e2c78> (NameError)
=20
The first lines of the program are:
class ::Array
def num_rows
return length <--- LINE 22
end
end
=20
a =3D Array.new
puts a.num_rows
=20
-- so how do I access ::Array.length?

class Array
alias :num_rows :length=20
end

[1,2,3].num_rows #=3D> 3

--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 
D

david

Cit=E1t Brian Schr=F6der said:
It's a web script, so I never noticed this warning in a class below t= he
array redef:
warning: redefining Object#initialize may cause infinite loop
=20
I had a "begin" in there that was unmatched, and a rescue several
functions later -
which was causing some real strange things.
=20
Anyway, now that I have put the begin in the same function as the res= cue,
I no longer get the error. But I still can't redefine Array - now I g= et:
=20
/var/www/ruby/core.rb:22:in `num_rows': undefined local variable or
method `length' for #<#<Module:0xb65fcfec>::Array:0xb65e2c78> (NameEr= ror)
=20
The first lines of the program are:
class ::Array
def num_rows
return length <--- LINE 22
end
end
=20
a =3D Array.new
puts a.num_rows
=20
-- so how do I access ::Array.length?
=20
class Array
alias :num_rows :length=20
end
=20
[1,2,3].num_rows #=3D> 3
=20
--=20
http://ruby.brian-schroeder.de/
=20
Stringed instrument chords: http://chordlist.brian-schroeder.de/
=20
=20

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?

David Vallner
 
D

david

Cit=E1t Leslie Viljoen said:
/var/www/ruby/core.rb:22:in `num_rows': undefined local variable or
method `length' for #<#<Module:0xb65fcfec>::Array:0xb65e2c78> (NameErro=
r)

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 whic=
h case
not even the aliasing would work properly.

I'm possibly summoning the Daemon Obfuscatis here, but this should work n=
o
matter what:

[].class.class_eval {
alias_method:)num_rows, :length)
}

Feel free to confuse yourself by using module_eval, or define_method.

David Vallner
 
P

Pit Capitain

Leslie said:
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)

You can see that you don't have an Array instance, but an instance of an
Array class in an anonymous module.
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?

Maybe you can show us more of the code and more than the first line of
the stack backtrace.

Regards,
Pit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top