Locking classes?

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?

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

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.

[email protected]

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.

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

(Sorry if this is a duplicate.)

On 2005.12.29 14:04, Gerardo S. Gómez Garrido
[email protected] 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 :slight_smile:

Gerardo S.

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

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

2005/12/28, Max E. [email protected]:

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

2005/12/28, Eero S. [email protected]:

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 :slight_smile:

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

On Thu, 29 Dec 2005 05:04:54 -0000, Steve L. [email protected]
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
#<URI::HTTP:0xfdbf049a8 URL:http://www.google.com>

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

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