ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> ruby-talk
ruby-talk
class destruction (evil genius metaprogramming)
by Giles Bowkett other posts by this author
Jun 14 2007 6:13PM messages near this date
Re: ruby File#sysread | Re: class destruction (evil genius metaprogramming)
I want to write a module which, when included in another module,
destroys every method of a class in that module. I realize this isn't
really a very useful thing to do, but I want to do it to see if it can
be done. I want to actually include the module and have it replace all
existing methods of this class with just one method.

In Python you can change an object's class in the middle of your
program by reassigning its magical __class__ variable. That would be a
very simple way to implement this idea. Doesn't seem possible here,
though.

Here's an attempt which failed:

> > class Muppet
> >   def show
> >     "it's the muppet show!"
> >     end
> >   end
=>  nil
> > kermit = Muppet.new
=>  #<Muppet:0x123129c>
> > kermit.show
=>  "it's the muppet show!"
> > class Muppet
> >   remove_method(:show)
> >   end
=>  Muppet
> > kermit.show
NoMethodError: undefined method `show' for #<Muppet:0x123129c> 
        from (irb):36
        from :0

So far so good, but I want to get rid of *every* method. Doing what I
just did programmatically, iterating over all available methods,
that's the thing which still eludes me.

None of these attempts work:

> > kermit.methods.each{|m| class << Muppet ; (remove_method(m)) ; end}
NameError: undefined local variable or method `m' for #<Class:Muppet> 
        from (irb):43
        from (irb):43:in `each'
        from (irb):43
        from :0
> > kermit.methods.each{|m| class Muppet; remove_method(m.to_sym) ; end}
NameError: undefined local variable or method `m' for Muppet:Class
        from (irb):44
        from (irb):44:in `each'
        from (irb):44
        from :0
> > kermit.methods.each{|m| Muppet.class_eval(remove_method(m))}
NoMethodError: undefined method `remove_method' for #<Object:0x349f4> 
        from (irb):45
        from (irb):45:in `each'
        from (irb):45
        from :0

I have to admit, it's not necessarily a bad thing if this task proves
impossible, but I feel like it *should* be possible.

-- 
Giles Bowkett

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Thread:
Giles Bowkett
Rick DeNatale
dblack
Robert Dober
dblack
Robert Dober
Giles Bowkett
dblack
Stephen Smith
Nathan Taylor-Hoover
Giles Bowkett
dblack
Giles Bowkett
Rick DeNatale
dblack
Devin Mullins
dblack
Gregory Brown
Giles Bowkett

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved