From: "Mariusz Pękala" Date: 2007-05-11T17:46:08+09:00 Subject: Re: instance_variables doesn't return unassigned variables --Nq2Wo0NMKNjxTN9z Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2007-05-11 06:58:48 +0900 (Fri, May), Robert Dober wrote: > On 5/10/07, Jesse Merriman wrote: > >On Thursday 10 May 2007 06:00, Bernd wrote: > >> Hi, > >> I wrote a code like this. > >> > >> class ABC > >> attr_accesor :first, :second > >> end > >> > >> when I wrote the following: > >> > >> x =3D ABC.new > >> x.first =3D "test" > >> puts x.instance_variables > >> > >> the output was only > >> > >> @first > > > >You could deduce them by looking for methods that end in an equals: > Hmm that might not be a good idea, a method ending in equals does not > induce the creation of an instance variable with that name. > One could however put attr on steroids by creating a list of > attributes for the class. > Look at this quick and dirty hack do see if this could be helpfull for you >=20 > class Module > alias_method :orig_attreader, :attr_reader > def attr_reader *symbols > @__atts__ ||=3D superclass.attributes.dup > @__atts__ +=3D symbols > @__atts__.uniq! > orig_attreader *symbols > end >=20 > def attributes > @__atts__ || [] > end > end >=20 =2E.or, since the original problem was that no instance variable has been created, just create it (in quick and dirty way): class Module alias_method :orig_attr, :attr def attr(symbol, writable =3D false) orig_attr symbol, writable send symbol end ... --=20 No virus found in this outgoing message. Checked by 'grep -i virus $MESSAGE' Trust me. --Nq2Wo0NMKNjxTN9z Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6-ecc01.6 (GNU/Linux) iD8DBQFGRC1AsnU0scoWZKARAgmtAKCCszqwNXPkELcybSxtpUDmu8oTZACglpwl dJ59dW/XXy70P1BE8v7+x1E= =t/PM -----END PGP SIGNATURE----- --Nq2Wo0NMKNjxTN9z--