From: niklas | brueckenschlaeger Date: 2010-11-15T18:43:17+09:00 Subject: Re: Mysql::Result .each_hash - unexpected result --=-imoFltc1vYe++5Te9SqX Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, 2010-11-15 at 18:30 +0900, Andy Tolle wrote: > Niklas, >=20 > Thanks for opening my eyes man. Saving it to an array makes the code=20 > dryer in less lines and makes it more proof to threaded environments.=20 Don't be too sure too soon :) http://coderrr.wordpress.com/2009/01/08/activerecord-threading-issues-and-r= esolutions/ > Seems well worth the extra line of code if you ask me. >=20 > Thanks for the help. >=20 > Andy >=20 >=20 > Niklas Cathor wrote in post #961493: > > On Mon, 2010-11-15 at 18:05 +0900, Andy Tolle wrote: > >> over his set? > > No, the database is not queried every time. The reason I was suggesting > > to use an array is that it that that way you don't need to handle that > > specially. It works like any other ruby array, unlike the Mysql::Result= , > > which you need to reset before iterating again. So if you're about to > > iterate over the result multiple times, possibly in different places > > it's save to either > > a) make your custom iterator, rewinding the result set afterwards > > OR > > b) use a standard ruby type, such as Array > > > > a) might look like this (ugly monkey patch): > > > > class Mysql::Result > > def my_each_hash(&block) > > each_hash(&block) ; data_seek(0) > > end > > end > > > > Be warned though: this might give you trouble in a threaded environment= . >=20 --=-imoFltc1vYe++5Te9SqX Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) iQIcBAABAgAGBQJM4QCvAAoJEIQdQGZOjo82sWYP/RM3o6hT5OA6A07UJwmOm7io Ba4WNdwZXSOkcqVmEXlXh2iRaNB6nKhLbEYIc9SNZwD2KvLpBdKuzC5oBL9qxYo9 mRf4kWFg+JIP4cCKxov5qqoebns/a5MkiTveYIVJzpXMpVsMRRvsk4GkVP6k5E2r sA8JZGQeWh/LyhTjSKd4RvthabfIFgoFhvODWSrAjEkKszXDrQa6vtdIJ62qbWq8 VElE/bgLzpbWyF2vvOdhSYZCsW15T9f5bz3uCI1ZFBsYLT+xMXZg3urnP4kuGndp wKxP/CH+YlrCDuqiU9YwfogMLSFPYWDJE3AECS2Y0mcDjqv4QakhRyoeJuDmY6z0 B0IpBR/HwjDaollWgJJ7O9IOasokORMa7p5cxLShK6fBlEVVQ1gFBSt1+NZNofy1 tYANQCxMpOr+HqU/tv3saEpf1Dy2JgGjiNWXgm5SlZz9Vd28zGnlMmKjEOkLLmyG /PUmTJw6ve4W1otdXE352QA0v2ch+Y2JNQZXlnM2hb1VT3GKbuMYg3gbbkD8RfW3 Qt9il1MCa14A3FwkYlGhU25ATETBU0pdpWW4IWcX2CWSpqvZvPC5U4eKJX0j4A9p vBZ5FtidCNMjTxoFB3OvsfBtVHeLYHbglZRxJnOTYwwSCV3yxXZ+OHRhYGmd65pz i7RCbF5it2pRbJxkDfTX =FGyu -----END PGP SIGNATURE----- --=-imoFltc1vYe++5Te9SqX--