From: Aaron Patterson Date: 2010-04-12T10:10:05+09:00 Subject: Re: attr_accessor, but for a boolean --qDbXVdCdHGoSgWSk Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Apr 12, 2010 at 10:00:06AM +0900, Albert Schlef wrote: > Let's say I have this code: >=20 > class Mambo > attr_accesstor :safe > end >=20 > Now, >=20 > 'safe', in my case, is a boolean. Problem is, the getter will be named > 'safe', whereas I want it to be 'safe?'. >=20 > In other words, how do I make the getter 'safe?' and the setter 'safe' > ? In situations like this, I make two "getters" and leave the assignment alone. Other ruby programmers expect your "setters" to be in the form of "obj.safe =3D true", not "obj.safe(true)". Try something like this: class Mambo attr_accesstor :safe alias :safe? :safe end Here it is in action: irb(main):001:0> class Mambo irb(main):002:1> attr_accessor :safe irb(main):003:1> alias :safe? :safe irb(main):004:1> end =3D> nil irb(main):005:0> m =3D Mambo.new =3D> # irb(main):006:0> m.safe =3D true =3D> true irb(main):007:0> m.safe? =3D> true irb(main):008:0> --=20 Aaron Patterson http://tenderlovemaking.com/ --qDbXVdCdHGoSgWSk Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (Darwin) iQEcBAEBAgAGBQJLwnLpAAoJEJUxcLy0/6/Gma8IAI3uRp74g1jLJgZvIitAizpD QCg+eYbkq8k43ciOe8tZey98cf3cdMgHDRdNNvX9wJ9xDFTe9kgFkzdOEuoquH1t rxgMAizJe4zhrw2j+vWVkFhs3PLLGc4vN7hNduQ7DumcuYDS7PXv0OjoWYmNwPcB oSuestIYpgfIJz5zlRKcsuGwJ8+x2Yn3WFgCmbuN31R5OwY+d6J+yi6NX23tI7Wq CPzjjfyGdfaRzJA27SpB8HFpzqpuHDc6L7IniniaX1RcaislxPQ7J3DX1lus2Q1q 7Mlj6+ncpHDdBO8dK+pEKsVEGvVwRg6E5Y3maSwXd4l6mt7OhXGPTDyfsrLmJJ4= =GiUg -----END PGP SIGNATURE----- --qDbXVdCdHGoSgWSk--