Locking classes?

M

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

Jack Christensen

Max said:
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
 
G

Gerardo Santana Gómez Garrido

2005/12/28 said:
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) # =3D> 2

A.freeze

#=3D> runtime error: "can't modify frozen object (TypeError)"
class A
def self.f(a)
a - 1
end
end

puts A.f(1)
 
S

Steve Litt

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 Litt
http://www.troubleshooters.com
(e-mail address removed)
 
G

gwtmp01

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 Wright
 
G

Gerardo Santana Gómez Garrido

2005/12/28 said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(Sorry if this is a duplicate.)



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.
 
R

Ross Bamford

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 #=> "<html> ...[snip]... </html>"
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top