[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Locking classes?

Max Eskin

12/29/2005 4:52:00 AM

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?

--
Posted via http://www.ruby-....


8 Answers

Jack Christensen

12/29/2005 5:02:00 AM

0

Max Eskin 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


Gerardo Santana Gómez Garrido

12/29/2005 5:04:00 AM

0

2005/12/28, Max Eskin <kurtkilgor@yahoo.com>:
> 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 Santana
"Between individuals, as between nations, respect for the rights of
others is peace" - Don Benito Juárez
http://santanatechnotes.blo...


Steve Litt

12/29/2005 5:05:00 AM

0

On Wednesday 28 December 2005 11:52 pm, Max Eskin 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 Litt
http://www.troublesh...
slitt@troubleshooters.com


ES

12/29/2005 5:34:00 AM

0




Gary Wright

12/29/2005 5:44:00 AM

0


On Dec 29, 2005, at 12:04 AM, Steve Litt 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 Wright





ES

12/29/2005 5:51:00 AM

0

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

(Sorry if this is a duplicate.)

On 2005.12.29 14:04, Gerardo Santana Gómez Garrido
<gerardo.santana@gmail.com> wrote:
> 2005/12/28, Max Eskin <kurtkilgor@yahoo.com>:
> > 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-----

Gerardo Santana Gómez Garrido

12/29/2005 6:08:00 AM

0

2005/12/28, Eero Saynatkari <ruby-ml@magical-cat.org>:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> (Sorry if this is a duplicate.)
>
> On 2005.12.29 14:04, Gerardo Santana Gómez Garrido
> <gerardo.santana@gmail.com> wrote:
> > 2005/12/28, Max Eskin <kurtkilgor@yahoo.com>:
> > > 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 :)

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

--
Gerardo Santana


Ross Bamford

12/29/2005 12:50:00 PM

0

On Thu, 29 Dec 2005 05:04:54 -0000, Steve Litt <slitt@earthlink.net> 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.googl...
#<URI::HTTP:0xfdbf049a8 URL:http://www.goog...

s = uri.read #=> "<html> ...[snip]... </html>"
s.class #=> String
s.base_uri #=> <URI::HTTP:0xfdbf1e54c URL:http://www.google....

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

--
Ross Bamford - rosco@roscopeco.remove.co.uk