Forum: Ruby Locking classes?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D29cd2024ca28fa646da91525d065e05?d=identicon&s=25 Max Eskin (kurtkilgor)
on 2005-12-29 05:52
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?
92926f8e4e94ac003e911c38cc4c0d0b?d=identicon&s=25 Jack Christensen (Guest)
on 2005-12-29 06:05
(Received via mailing list)
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
5a601582df3b42b65a5e8353fc9305da?d=identicon&s=25 =?ISO-8859-1?Q?Gerardo_Santana_G=F3mez_Garrido?= (Guest)
on 2005-12-29 06:05
(Received via mailing list)
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.blogspot.com/
1b62a85b59ccab03b84ee5ec378f75b4?d=identicon&s=25 Steve Litt (Guest)
on 2005-12-29 06:05
(Received via mailing list)
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.troubleshooters.com
slitt@troubleshooters.com
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 unknown (Guest)
on 2005-12-29 06:45
(Received via mailing list)
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
5c7bdd14d6885c8275eaf78be41d120a?d=identicon&s=25 Eero Saynatkari (Guest)
on 2005-12-29 06:51
(Received via mailing list)
-----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:
>   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 Santana


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

iD8DBQFDs4JexvA1l6h+MUMRAroyAJ9x0rW/qfogpLWBwPTv/ISMvA4UfQCdEEts
UyA68gyAI230davGOmEIJ/A=
=j3Bn
-----END PGP SIGNATURE-----
5a601582df3b42b65a5e8353fc9305da?d=identicon&s=25 =?ISO-8859-1?Q?Gerardo_Santana_G=F3mez_Garrido?= (Guest)
on 2005-12-29 07:09
(Received via mailing list)
2005/12/28, Eero Saynatkari <ruby-ml@magical-cat.org>:
> > > 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.
82e62c756d89bc6fa0a0a2d7f2b1e617?d=identicon&s=25 Ross Bamford (Guest)
on 2005-12-29 13:59
(Received via mailing list)
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.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.
This topic is locked and can not be replied to.