From: "Esteban Manchado Velázquez" Date: 2006-05-03T17:28:48+09:00 Subject: Re: Constant in Ruby. --EVF5PPMfhYS0aIcm Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hi Charlie, On Sun, Apr 30, 2006 at 03:57:41PM +0900, Charlie wrote: > [...] > > It's constant in that you can't reassign a different instance to the=20 > > same name (try setting X =3D 10 after you've set it to 20 and Ruby will= =20 > > gripe). The instances themselves may still be mutable (see=20 > > Object#freeze if you want to change that). >=20 > okay, I just try this: >=20 > X =3D 20 > X =3D 10 > puts(X) > 10 > X.freeze > X =3D 30 > puts(X) > 30 The "freeze" doesn't let the object change, but in this case you're not changing the object, you're changing the reference. Let's see if an example clears it up: ------------------------------------- 8< ----------------------------------= --- irb(main):001:0> X =3D "hi" =3D> "hi" irb(main):002:0> X =3D "hello" (irb):2: warning: already initialized constant X =3D> "hello" irb(main):003:0> X.sub!(/$/, "!") =3D> "hello!" irb(main):004:0> X.freeze =3D> "hello!" irb(main):005:0> X.sub!(/e/, "a") TypeError: can't modify frozen string from (irb):5:in `sub!' from (irb):5 irb(main):006:0> ------------------------------------- >8 ----------------------------------= --- You can't reassign a constant (without a warning), and you can't change an _object_ (possibly, but not necessarily, pointed to by a constant) that has been frozen. HTH, --=20 Esteban Manchado Vel=E1zquez - http://www.foton.es EuropeSwPatentFree - http://EuropeSwPatentFree.hispalinux.es --EVF5PPMfhYS0aIcm Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.3 (GNU/Linux) iD8DBQFEVKbthYgK5b1UDsERAgBKAJ4mreTQ+PTHNG7VPdN9Jgeg4SrPlQCaA7UD FU37HgCkDHo33LD/e3AJpyo= =oPSf -----END PGP SIGNATURE----- --EVF5PPMfhYS0aIcm--