[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

how to reset and how to undefine a class

Adriano Mitre

10/30/2006 5:16:00 AM

Does any one know how to reset a class in Ruby? Subsequent class
definitions don't replace the original, but are tipically appended to
it.

The following example is typical.

#########################
class Song
def play
puts "playing..."
end
end

s = Song.new

class Song
def stop
puts "stopped."
end
end

s.play #=> "playing..."
s.stop #=> "stopped."
#########################

How can I make s.stop produce a NoMethodError, ie., how can I reset Song
class prior to redefining it?

Besides, I would like to know how to undefine Song class, so that
Song.new produce a NameError, i.e., behaving as if it were never
defined.

Thanks!

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

7 Answers

Wilson Bilkovich

10/30/2006 5:24:00 AM

0

On 10/30/06, Adriano Mitre <adriano.mitre@gmail.com> wrote:
> Does any one know how to reset a class in Ruby? Subsequent class
> definitions don't replace the original, but are tipically appended to
> it.
>
> The following example is typical.
>
> #########################
> class Song
> def play
> puts "playing..."
> end
> end
>
> s = Song.new
>
> class Song
> def stop
> puts "stopped."
> end
> end
>
> s.play #=> "playing..."
> s.stop #=> "stopped."
> #########################
>
> How can I make s.stop produce a NoMethodError, ie., how can I reset Song
> class prior to redefining it?
>
> Besides, I would like to know how to undefine Song class, so that
> Song.new produce a NameError, i.e., behaving as if it were never
> defined.
>

The first question is (a little) straightforward, once you get a
handle on how open Ruby's classes are.
To remove the 'stop' method, 'reopen' the class as you did when adding
'stop' in the first place:

class Song
undef stop
end

Removing the whole 'Song' constant is a little weirder. There is a
remove_const method, but you have to call it on the thing that
contains the constant you want to remove.
In this case, the thing above Song is Object.

Object.send(:remove_const, :Song)

Sorry for my poor explanation. Hopefully you won't need to do this
very often. I never have. Heh.

Trans

10/30/2006 12:18:00 PM

0


Adriano Mitre wrote:

> Besides, I would like to know how to undefine Song class, so that
> Song.new produce a NameError, i.e., behaving as if it were never
> defined.

You can redefine a class entirely by giving it a new subclass.

class Song < Object
end

T.

Trans

10/30/2006 12:22:00 PM

0


Trans wrote:
> Adriano Mitre wrote:
>
> > Besides, I would like to know how to undefine Song class, so that
> > Song.new produce a NameError, i.e., behaving as if it were never
> > defined.
>
> You can redefine a class entirely by giving it a new subclass.
>
> class Song < Object
> end

s/subclass/superclass/

T.

Ross Bamford

10/30/2006 2:01:00 PM

0

On Mon, 2006-10-30 at 22:30 +0900, Trans wrote:
> Trans wrote:
> > Adriano Mitre wrote:
> >
> > > Besides, I would like to know how to undefine Song class, so that
> > > Song.new produce a NameError, i.e., behaving as if it were never
> > > defined.
> >
> > You can redefine a class entirely by giving it a new subclass.
> >
> > class Song < Object
> > end
>
> s/subclass/superclass/

I don't get it:

class SomeClass < String
def ameth(a)
p a
end
end

class SomeClass < Object
end

SomeClass.new.ameth(10)


:w !ruby
-:7: superclass mismatch for class SomeClass (TypeError)

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


Trans

10/30/2006 6:04:00 PM

0


Ross Bamford wrote:
> On Mon, 2006-10-30 at 22:30 +0900, Trans wrote:
> > Trans wrote:
> > > Adriano Mitre wrote:
> > >
> > > > Besides, I would like to know how to undefine Song class, so that
> > > > Song.new produce a NameError, i.e., behaving as if it were never
> > > > defined.
> > >
> > > You can redefine a class entirely by giving it a new subclass.
> > >
> > > class Song < Object
> > > end
> >
> > s/subclass/superclass/
>
> I don't get it:
>
> class SomeClass < String
> def ameth(a)
> p a
> end
> end
>
> class SomeClass < Object
> end
>
> SomeClass.new.ameth(10)
>
>
> :w !ruby
> -:7: superclass mismatch for class SomeClass (TypeError)

Hmph.... Looks like Ruby changed this at some point. It used to be able
to do that.

T.


Dido Sevilla

10/31/2006 1:52:00 AM

0

On 10/30/06, Adriano Mitre <adriano.mitre@gmail.com> wrote:
> How can I make s.stop produce a NoMethodError, ie., how can I reset Song
> class prior to redefining it?
>
> Besides, I would like to know how to undefine Song class, so that
> Song.new produce a NameError, i.e., behaving as if it were never
> defined.

All class definitions are constants in the Object class. So by doing
the following:

class Song
end

class Object
remove_const :Song
end

Song.new
NameError: uninitialized constant Song

classes can be undefined. You could also alternatively say:

Object.send(:remove_const, :Song)

Adriano Mitre

11/7/2006 10:06:00 AM

0

> The first question is (a little) straightforward, once you get a
> handle on how open Ruby's classes are.
> To remove the 'stop' method, 'reopen' the class as you did when adding
> 'stop' in the first place:
>
> class Song
> undef stop
> end

I want to remove ("undef") all methods, no only a single one. That is
what I meant with "reset a class".

> Removing the whole 'Song' constant is a little weirder. There is a
> remove_const method, but you have to call it on the thing that
> contains the constant you want to remove.
> In this case, the thing above Song is Object.
>
> Object.send(:remove_const, :Song)
>
> Sorry for my poor explanation. Hopefully you won't need to do this
> very often. I never have. Heh.

It is not exactly elegant, but indeed I think I won't need it often. I
am inclined to using the "Object.send(:remove_const, :classname)"
solution in both cases, reset and undef.

Thank you very much.

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