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?

···

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

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

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)

···

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?

--
Gerardo Santana
"Between individuals, as between nations, respect for the rights of
others is peace" - Don Benito Juárez

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

slitt@troubleshooters.com

···

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?

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

···

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?

(Sorry if this is a duplicate.)

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

Gerardo Santana

E

···

On 2005.12.29 14:04, Gerardo Santana Gómez Garrido <gerardo.santana@gmail.com> wrote:

2005/12/28, Max Eskin <kurtkilgor@yahoo.com>:

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/&gt;

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

···

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?

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

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

···

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

--
Gerardo Santana