# Locking classes?

**URL:** <https://www.ruby-forum.com/t/locking-classes/51294>\
**Category:** Ruby\
**Created:** [December 29, 2005, 5:52am UTC](https://www.ruby-forum.com/t/locking-classes/51294 "2005-12-29T05:52:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kurtkilgor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kurtkilgor](https://www.ruby-forum.com/u/kurtkilgor)\
**Post date:** [December 29, 2005, 5:52am UTC](https://www.ruby-forum.com/t/locking-classes/51294/1 "2005-12-29T05:52:14Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kurtkilgor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kurtkilgor](https://www.ruby-forum.com/u/kurtkilgor)\
**Post date:** [December 29, 2005, 6:05am UTC](https://www.ruby-forum.com/t/locking-classes/51294/2 "2005-12-29T06:05:26Z")

</div>

Max E. wrote:

> 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?

freeze the class. Example:

irb(main):001:0\> class A  
irb(main):002:1\> def f  
irb(main):003:2\> 42  
irb(main):004:2\> end  
irb(main):005:1\> end  
=\> nil  
irb(main):006:0\> A.freeze  
=\> A  
irb(main):007:0\> class A  
irb(main):008:1\> def g  
irb(main):009:2\> 43  
irb(main):010:2\> end  
irb(main):011:1\> end  
TypeError: can’t modify frozen class  
from (irb):8

Jack

---

<div class="post-metadata">

**Author:** ![kurtkilgor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kurtkilgor](https://www.ruby-forum.com/u/kurtkilgor)\
**Post date:** [December 29, 2005, 6:05am UTC](https://www.ruby-forum.com/t/locking-classes/51294/3 "2005-12-29T06:05:30Z")

</div>

On Wednesday 28 December 2005 11:52 pm, Max E. wrote:

> 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?

Yeah, on a similar subject, under what circumstances would one want to  
add a  
method to an object if that method wasn’t part of its class?

SteveT

Steve L.

> **[Troubleshooters.Com](http://www.troubleshooters.com)**
>
> The process and mindset of troubleshooting.

removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![kurtkilgor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kurtkilgor](https://www.ruby-forum.com/u/kurtkilgor)\
**Post date:** [December 29, 2005, 6:45am UTC](https://www.ruby-forum.com/t/locking-classes/51294/4 "2005-12-29T06:45:41Z")

</div>

On Dec 29, 2005, at 12:04 AM, Steve L. wrote:

> Yeah, on a similar subject, under what circumstances would one want  
> to add a  
> method to an object if that method wasn’t part of its class?

I’m going to use the term ‘singleton method’ to mean a method associated  
with a single object.

Creating a singleton method on a class object is the idiom for creating  
‘class methods’ in Ruby. For example consider Date.today. It doesn’t  
make sense to define the method ‘today’ in class ‘Class’ because we  
don’t  
want _all_ classes to respond to ‘today’ but just one particular  
class known as ‘Date’. So we make Date.today a singleton method on the  
class object known as ‘Date’.

Another common use of singleton methods is to override a method  
definition  
for one particular object. In other languages this might be accomplished  
by explicitly defining a subclass with the new method definition and  
then  
creating an instance. In Ruby, you create a regular instance of the  
base  
class and then define a singleton method to override the definition from  
the class.

Gary W.

---

<div class="post-metadata">

**Author:** ![kurtkilgor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kurtkilgor](https://www.ruby-forum.com/u/kurtkilgor)\
**Post date:** [December 29, 2005, 6:51am UTC](https://www.ruby-forum.com/t/locking-classes/51294/5 "2005-12-29T06:51:42Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

(Sorry if this is a duplicate.)

On 2005.12.29 14:04, Gerardo S. Gómez Garrido  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def self.f(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 S.

E  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDs4JexvA1l6h+MUMRAroyAJ9x0rW/qfogpLWBwPTv/ISMvA4UfQCdEEts  
UyA68gyAI230davGOmEIJ/A=  
=j3Bn  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![kurtkilgor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kurtkilgor](https://www.ruby-forum.com/u/kurtkilgor)\
**Post date:** [December 29, 2005, 6:05am UTC](https://www.ruby-forum.com/t/locking-classes/51294/6 "2005-12-29T06:05:28Z")

</div>

2005/12/28, Max E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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)

–  
Gerardo S.  
“Between individuals, as between nations, respect for the rights of  
others is peace” - Don Benito Juárez

> **[Santana's Tech Notes](http://santanatechnotes.blogspot.com/)**
>
> Small notes about technical related stuff I do, mainly OpenBSD and Ruby

---

<div class="post-metadata">

**Author:** ![kurtkilgor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kurtkilgor](https://www.ruby-forum.com/u/kurtkilgor)\
**Post date:** [December 29, 2005, 7:09am UTC](https://www.ruby-forum.com/t/locking-classes/51294/7 "2005-12-29T07:09:45Z")

</div>

2005/12/28, Eero S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > declared, is there any way to lock it to prevent further methods from
> > 
> > puts A.f(1)
> 
> One can still A.dup, but I think this is a reasonable way 🙂

The original poster was concerned about adding methods. In such case  
#dup is inoffensive.

---

<div class="post-metadata">

**Author:** ![kurtkilgor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kurtkilgor](https://www.ruby-forum.com/u/kurtkilgor)\
**Post date:** [December 29, 2005, 1:59pm UTC](https://www.ruby-forum.com/t/locking-classes/51294/8 "2005-12-29T13:59:32Z")

</div>

On Thu, 29 Dec 2005 05:04:54 -0000, Steve L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Yeah, on a similar subject, under what circumstances would one want to  
> add a  
> method to an object if that method wasn’t part of its class?

Check out the way open-uri returns an extended string:

require ‘open-uri’

uri = URI.parse ‘[http://www.google.com](http://www.google.com)’  
#\<URI::HTTP:0xfdbf049a8 URL:[http://www.google.com](http://www.google.com)\>

s = uri.read #=\> “ …[snip]… ”  
s.class #=\> String  
s.base\_uri #=\> \<URI::HTTP:0xfdbf1e54c  
URL:[http://www.google.co.uk/](http://www.google.co.uk/)\>

for one example. It’s actually several methods in this case, from a  
module.
