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