From: Aaron Patterson Date: 2012-05-04T00:23:20+09:00 Subject: [ruby-core:44850] Re: help understanding weird bug --pWyiEgJYm5f9v55/ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, May 03, 2012 at 08:56:42PM +0900, Rodrigo Rosenfeld Rosas wrote: > Here is a sample application: >=20 > https://github.com/rosenfeld/weird-sequel-bug >=20 > And here is the strange behavior: >=20 > https://github.com/rosenfeld/weird-sequel-bug/blob/master/example.rb >=20 > children =3D Sq::Node[1].children > p children.class > parent =3D children.first.parent > p children.map &:id > 2.times { p children.map{|c| c.parent =3D parent; c.id} } >=20 > Output: >=20 > Array > [2, 3] > [2, 2] > [3, 3] >=20 > For some strange reason, changing the parent of the element inside > children.map{} block will modify the children array itself. >=20 > for example: >=20 > p children.map &:id > children[0].parent =3D parent > p children.map &:id >=20 > outputs: > [2, 3] > [3, 2] >=20 > Maybe this is a Sequel bug (I didn't look for the code of > associations yet), but I'd like to understand how this could happen > in Ruby. >=20 > For example, how can I know if this is an ordinary Array or not? > children.class is Array, so how can I differentiate it from a normal > Array. Checking the class is a good start, but I would also check the ancestors in case anything has been mixed in: irb(main):001:0> x =3D [] =3D> [] irb(main):002:0> x.extend(Module.new { def [](z); 'hi'; end }) =3D> [] irb(main):003:0> x.class =3D> Array irb(main):004:0> x[1] =3D> "hi" irb(main):005:0> class << x; self; end.ancestors =3D> [#, Array, Enumerable, Object, Kernel, Ba= sicObject] irb(main):006:0> That said, for better help, you should send this to ruby-talk or the sequel list. --=20 Aaron Patterson http://tenderlovemaking.com/ --pWyiEgJYm5f9v55/ Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (Darwin) iQEcBAEBAgAGBQJPoqLjAAoJEJUxcLy0/6/GcfQH/0x4DOLkha8rDON1WdQfxu8f yfXV9XwEvAZ55PcPZ7OtfdxEsVO50LNrHwAWdNVfmNmy5RvCyPgmZYCZnzooDq+q wc0xL+RafmSE1Z7EF8uKSUDaOPxMVPSPlN/+N6fpvmFjS5SiA+q8Faq8UeEj6j3j 5RONz8atnTKOqolx/RrHB4m1o4HXLu5qaDWFPON8ij6p4og8xN2eOVzVVItZyyat FolwEo3ubYYgZqnkuf+mjHIpan+pjvqLMn3ARB59YR0KVEb8le2Oqy/F4hsC1ike CSdLk5Gole+VJX1//zxNoxcpAn46ejhRSsb/3z251WIqUimCgk5vsaHBNnSAxr8= =41r+ -----END PGP SIGNATURE----- --pWyiEgJYm5f9v55/--