From: Eero Saynatkari Date: 2005-12-29T14:51:02+09:00 Subject: Re: Locking classes? -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 (Sorry if this is a duplicate.) On 2005.12.29 14:04, Gerardo Santana G�mez Garrido wrote: > 2005/12/28, Max Eskin : > > Hi, > > I am just starting to use Ruby. I'm wondering, once a class has been > > declared, is there any way to lock it to prevent further methods from > > being defined? In other words is there a way to make Ruby act like a > > static language for some classes? > > Use Module#freeze > > class A > def self.f(a) > a + 1 > end > end > > puts A.f(1) # => 2 > > A.freeze > > #=> runtime error: "can't modify frozen object (TypeError)" > class A > def self.f(a) > a - 1 > end > end > > puts A.f(1) One can still A.dup, but I think this is a reasonable way :) > Gerardo Santana E -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) iD8DBQFDs4JexvA1l6h+MUMRAroyAJ9x0rW/qfogpLWBwPTv/ISMvA4UfQCdEEts UyA68gyAI230davGOmEIJ/A= =j3Bn -----END PGP SIGNATURE-----