From: David Pollak Date: 2006-05-04T01:07:02+09:00 Subject: Re: Constant in Ruby. ------=_Part_48009_24948603.1146672418843 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline You're freezing the Object that is assigned to X. If you want to freeze th= e class that owns X, you can try: class Foo X =3D "Hello".freeze # nobody can change the string out from under us end Foo.freeze # now, you can't change the Foo class That should do what you want. On 4/29/06, Charlie wrote: > > Mike Fletcher wrote: > > Charlie wrote: > >> I'm new to Ruby programming and I saw this article which concerns me: > >> > >> http://www.bitwisemag.com/copy/features/opinion/ruby/ruby_debate.html > >> > >> Don't get me wrong, I love Ruby. But, the idea that a constant is not = a > >> constant is worrying isn't it? Or have I missed something? Please > >> comment. > >> > >> X =3D 10 > >> Y =3D "hello world" > >> Z =3D "hello world" > >> > >> X =3D 20 > >> Y =3D "byebye" << "abc" > >> myvar =3D Z << "xyz" > >> > >> puts(X) > >> 20 > >> puts(Y) > >> byebyeabc > >> puts(Z) > >> hello worldxyz > >> myvar > >> "hello worldxyz" > > > > It's constant in that you can't reassign a different instance to the > > same name (try setting X =3D 10 after you've set it to 20 and Ruby will > > gripe). The instances themselves may still be mutable (see > > Object#freeze if you want to change that). > > okay, I just try this: > > X =3D 20 > X =3D 10 > puts(X) > 10 > X.freeze > X =3D 30 > puts(X) > 30 > > Sorry, but I find it difficult to understand as I'm new to it. Would > be helpful if you can enlighten me with some examples. Thanks. > > > > > -- > Posted via http://www.ruby-forum.com/. > > -- -------- David Pollak's Ruby Playground http://dppruby.com ------=_Part_48009_24948603.1146672418843--