[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Array#size empties the Array??

Sven Suska

8/3/2006 8:51:00 AM

Hello,

I've encountered the following strang behaviour of Array#size.
The example is in a context of ActiveRecord, so I don't know
if it is an ActiveRecord or Ruby issue (or something even more
bizarre...)

I have taken it out of context, but: Can you think of _ANY_ context,
where the following behaviour makes any sense whatsoever or is
at least explainable?


First, consider this code piece:

p [:rks_length, rks.length]
p [:rks_size, rks.size]
p [:rks_class, rks.class]
p [:rks_item, rks[0]]
p [:rks_length, rks.length]
p [:rks_size, rks.size]

It produces this output:

[:rks_length, 31]
[:rks_size, 31]
[:rks_class, Array]
[:rks_item, #<Rk:0x16344d8 @attributes={"rzvk"=>0.0, ...}>]
[:rks_length, 31]
[:rks_size, 31]


Now, imagine the above code piece is replaced
by the following, everything else remaining _exacly_ the same:

p [:rks_size, rks.size]
p [:rks_length, rks.length]
p [:rks_class, rks.class]
p [:rks_item, rks[0]]
p [:rks_length, rks.length]
p [:rks_size, rks.size]

The output will then be:

[:rks_size, 0]
[:rks_length, 0]
[:rks_class, Array]
[:rks_item, nil]
[:rks_length, 0]
[:rks_size, 0]


And this is not a singular incidence, but it occurs repeatedly
always in the same way.
I have also tried variations on this, and observed the following:
Every time when #size was the first method
that was sent to the object "rks",
the object would be empty.
For example, sending #class first and then #size
would already save rks from destruction.

I've tried to read if there was a difference between Array#size
and Array#length, but I only found that #size is an alias for #length.
Then I looked if it might be due to some redefinition in ActiveSupport
but I have not found anything there.

The context of the example also includes Apollo and Test::Unit,
maybe they do something strange to Array#size??

At this point I wish to have some way of telling where (in which file)
a particular method was defined - is there already a means of getting
this information?


Any enlightenment appreciated

Sven

--
Posted via http://www.ruby-....

16 Answers

Sven Suska

8/3/2006 9:05:00 AM

0

Sorry, I had forgotten to give enough information:


1. rks is a local variable!

The statement sequence is actually:

rks = vt.rks
p [:rks_length, rks.length]
p [:rks_size, rks.size]
...

And this is inside a Test::Unit class, where I have not defined a
rks= writer method.


2. Versions

Ruby version is 1.8.4 on WindowsXP (one-click-installer).
ActiveRecord 1.14.1
Apollo 0.841a_vcl60


Regards

Sven

--
Posted via http://www.ruby-....

Sven Suska

8/3/2006 11:48:00 AM

0

Robert Dober wrote:
> try not to replace the code, which is equivalent, and run your test
> twice
> with the same code.
> Cheers
> Robert

Dear Robert,

thank you for your quick reply.

Although...,
being so quick may mean:
perhaps too little time to get the point
I was trying to make? ;-)

You say the code _is_ equivalent.
And I say the code _should_be_ equivalent.
I definitively found out, that it does not behave "equivalently".


You are suggesting to run my test twice.
What do you mean by "twice"?
Of course, I had already run it plenty of times ...,
But anyway, now I have done the following:

I copied-and-pasted the whole test-method
that contained the mentioned code lines
inside my TestCase class, like this:

class WierdSizeTest < Test::Unit::TestCase
def test1
...
rks = vt.rks
p [:rks_length, rks.length]
p [:rks_size, rks.size]
p [:rks_class, rks.class]
p [:rks_item, rks[0]]
p [:rks_length, rks.length]
p [:rks_size, rks.size]
...
end

def test2
...
rks = vt.rks
p [:rks_length, rks.length]
p [:rks_size, rks.size]
p [:rks_class, rks.class]
p [:rks_item, rks[0]]
p [:rks_length, rks.length]
p [:rks_size, rks.size]
...
end

And I also repeated the test with the "equivalent" code exchanged,
ie swapped method calls of #length and #size.


And what I found was this:

The only factor that influenced the output was
_the_order_of_the_method_calls_.

Or, to put it directly:
Whenever rks.size was the _first_ method call on "rks",
the output was:
[:rks_size, 0]
[:rks_length, 0]
[:rks_class, Array]
[:rks_item, nil]
[:rks_length, 0]
[:rks_size, 0]
And the output was normal otherwise. (as posted in the beginning)

Regardless of how many times a test method was repeated.
Regardless of the order of test methods. (When one method
contained the "corrupted" variant and the other did not.)


This all stabilizes the hypothesis,
that the Array#size method empties the array
when called as the first method after the object obtained.


Any more suggestions?


Cheers

Sven


> --
> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
> concerne l'univers, je n'en ai pas acquis la certitude absolue.

Well, it seems so ... - but how does this affect this particular
case?...
:-)


--
Posted via http://www.ruby-....

Alex Young

8/3/2006 11:54:00 AM

0

Sven Suska wrote:
<snip>
> Or, to put it directly:
> Whenever rks.size was the _first_ method call on "rks",
> the output was:
> [:rks_size, 0]
> [:rks_length, 0]
> [:rks_class, Array]
> [:rks_item, nil]
> [:rks_length, 0]
> [:rks_size, 0]
> And the output was normal otherwise. (as posted in the beginning)
>
> Regardless of how many times a test method was repeated.
> Regardless of the order of test methods. (When one method
> contained the "corrupted" variant and the other did not.)
>
>
> This all stabilizes the hypothesis,
> that the Array#size method empties the array
> when called as the first method after the object obtained.
This may be irrelevant, but what's rks.class.ancestors?

--
Alex

Sven Suska

8/3/2006 12:00:00 PM

0

Sven Suska wrote:
> when called as the first method after the object obtained.

Sorry, I ment:

when called as the first method after the object is obtained.





I didn't say "created" because it is created somewhere inside
ActiveRecord,
so I just wanted to say "obtained from ActiveRecord".



Sven

--
Posted via http://www.ruby-....

Sven Suska

8/3/2006 12:37:00 PM

0

Alex Young wrote:

> Sven Suska wrote:
>> This all stabilizes the hypothesis,
>> that the Array#size method empties the array
>> when called as the first method after the object obtained.

> This may be irrelevant, but what's rks.class.ancestors?

[Array,
ActiveSupport::CoreExtensions::Array::Conversions,
Enumerable,
Object,
Kernel]

Sven

--
Posted via http://www.ruby-....

Rob Sanheim

8/3/2006 1:06:00 PM

0

On 8/3/06, Sven Suska <software617rf@suska.org> wrote:
> Robert Dober wrote:
> > try not to replace the code, which is equivalent, and run your test
[snip]
> Well, it seems so ... - but how does this affect this particular
> case?...
> :-)
>
>

I think you should post (or send a link to) the full test case in
quesiton, including the source you have for whatever model makes up
'rks'.

- Rob
--
http://www.robs...
http://www.seekin...
http://www.a...

Sven Suska

8/3/2006 1:16:00 PM

0

Rob Sanheim wrote:
> I think you should post (or send a link to) the full test case in
> quesiton, including the source you have for whatever model makes up
> 'rks'.

Well, it's so big...

I'll try something else first.

Sven


--
Posted via http://www.ruby-....

Yukihiro Matsumoto

8/3/2006 1:57:00 PM

0

Hi,

In message "Re: Array#size empties the Array??"
on Thu, 3 Aug 2006 18:04:41 +0900, Sven Suska <software617rf@suska.org> writes:

|Sorry, I had forgotten to give enough information:

This is not enough. Could you show us a whole script to reproduce the
problem? I am sure your expectation of "exactly same" fails
somewhere.

matz.

Sven Suska

8/3/2006 4:01:00 PM

0

Yukihiro Matsumoto wrote:
> Sven Suska writes:
>
> |Sorry, I had forgotten to give enough information:
>
> This is not enough. Could you show us a whole script to reproduce the
> problem?

Yes I know it would be useful, but it's hard to find and extract the
relevant
parts.

> I am sure your expectation of "exactly same" fails
> somewhere.

Well, sorry to say that, but I think your assumption is not correct.



Let's see if my new findings give anybody a clue.


At first I tried a different ActiveRecord version - no difference.

Then I put some debug printouts into AR and I found clearly different
behaviour.
I'll just give the raw output,
even if I don't tell you what the printout statements were,
you can see the difference clearly:


1. #length first:
================

** RUBY_VERSION = "1.8.4"
** "ar-svs: args=[:all]"
** [:SVSAR_find_sql, "SELECT * FROM rkScGla WHERE (rkScGla.vsnr =
'GK_715') AND (rkScGla.komp = 'R1') AND (rkScGla.vtnr = 1) "]
** "tn=rkScGla"
** :vor_newtable
** :nach_newtable
** "tn=rkScGla"
** :vor_newtable
** :nach_newtable
** "tn=rkScGla"
** :vor_newtable
** :nach_newtable
[:rks_length, 31]
[:rks_size, 31]
[:rks_class, Array]
[:rks_item, #<Rk:0x16344d8 @attributes={"rzvk"=>0.0, ...}>]
[:rks_length, 31]
[:rks_size, 31]



2. #size first:
===============

** RUBY_VERSION = "1.8.4"
** "tn=rkScGla"
** :vor_newtable
** :nach_newtable
** "tn=rkScGla"
** :vor_newtable
** :nach_newtable
** "tn=rkScGla"
** :vor_newtable
** :nach_newtable
** [:SVSAR_sel_1_sql, "SELECT count(*) AS count_all FROM rkScGla WHERE
(rkScGla.vsnr = 'GK_715' AND rkScGla.komp = 'R1' AND rkScGla.vtnr = 1) "
[:rks_size, 0]
[:rks_length, 0]
[:rks_class, Array]
[:rks_item, nil]
[:rks_length, 0]
[:rks_size, 0]



---> This looks like a very exciting ActiveRecord hack!!!

It looks like rks is something special in the beginning,
that will be replaced by the real array as soon as methods are
being sent to it.

Well, whatever the explanation may be, it has become clear now,
that this behaviour is caused by ActiveRecord, and so I should
repost the issue in the rails forum.

And now I understand why this issue has not bothered anybody before,
it has only come up because my hand-written database-adapter is not
yet able to execute the "SELECT count(*) ..." statement correctly.


OK... Exciting day, today...


Thank you all for your support


Regards

Sven






--
Posted via http://www.ruby-....

Logan Capaldo

8/3/2006 4:09:00 PM

0


On Aug 3, 2006, at 12:00 PM, Sven Suska wrote:

> Yukihiro Matsumoto wrote:
>> Sven Suska writes:
>>
>> |Sorry, I had forgotten to give enough information:
>>
>> This is not enough. Could you show us a whole script to reproduce
>> the
>> problem?
>
> Yes I know it would be useful, but it's hard to find and extract the
> relevant
> parts.
>
>> I am sure your expectation of "exactly same" fails
>> somewhere.
>
> Well, sorry to say that, but I think your assumption is not correct.
>
>
>
> Let's see if my new findings give anybody a clue.
>
>
> At first I tried a different ActiveRecord version - no difference.
>
> Then I put some debug printouts into AR and I found clearly different
> behaviour.
> I'll just give the raw output,
> even if I don't tell you what the printout statements were,
> you can see the difference clearly:
>
>
> 1. #length first:
> ================
>
> ** RUBY_VERSION = "1.8.4"
> ** "ar-svs: args=[:all]"
> ** [:SVSAR_find_sql, "SELECT * FROM rkScGla WHERE (rkScGla.vsnr =
> 'GK_715') AND (rkScGla.komp = 'R1') AND (rkScGla.vtnr = 1) "]
> ** "tn=rkScGla"
> ** :vor_newtable
> ** :nach_newtable
> ** "tn=rkScGla"
> ** :vor_newtable
> ** :nach_newtable
> ** "tn=rkScGla"
> ** :vor_newtable
> ** :nach_newtable
> [:rks_length, 31]
> [:rks_size, 31]
> [:rks_class, Array]
> [:rks_item, #<Rk:0x16344d8 @attributes={"rzvk"=>0.0, ...}>]
> [:rks_length, 31]
> [:rks_size, 31]
>
>
>
> 2. #size first:
> ===============
>
> ** RUBY_VERSION = "1.8.4"
> ** "tn=rkScGla"
> ** :vor_newtable
> ** :nach_newtable
> ** "tn=rkScGla"
> ** :vor_newtable
> ** :nach_newtable
> ** "tn=rkScGla"
> ** :vor_newtable
> ** :nach_newtable
> ** [:SVSAR_sel_1_sql, "SELECT count(*) AS count_all FROM rkScGla WHERE
> (rkScGla.vsnr = 'GK_715' AND rkScGla.komp = 'R1' AND rkScGla.vtnr =
> 1) "
> [:rks_size, 0]
> [:rks_length, 0]
> [:rks_class, Array]
> [:rks_item, nil]
> [:rks_length, 0]
> [:rks_size, 0]
>
>
>
> ---> This looks like a very exciting ActiveRecord hack!!!
>
> It looks like rks is something special in the beginning,
> that will be replaced by the real array as soon as methods are
> being sent to it.
>
> Well, whatever the explanation may be, it has become clear now,
> that this behaviour is caused by ActiveRecord, and so I should
> repost the issue in the rails forum.
>
> And now I understand why this issue has not bothered anybody before,
> it has only come up because my hand-written database-adapter is not
> yet able to execute the "SELECT count(*) ..." statement correctly.
>
>
> OK... Exciting day, today...
>
>
> Thank you all for your support
>
>
> Regards
>
> Sven
>
>

AR does all sorts of magic. It has been my experience that whenever
something doesn't work as it should or as I expect and I am making
use of AR, AR is probably the culprit.

>
>
>
>
> --
> Posted via http://www.ruby-....
>