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
Re: class destruction (evil genius metaprogramming)
by dblack other posts by this author
Jun 15 2007 6:55AM messages near this date
Re: class destruction (evil genius metaprogramming) | Re: class destruction (evil genius metaprogramming)
Hi --

On Fri, 15 Jun 2007, Giles Bowkett wrote:

> > kermit.class.instance_methods(false).each{|m|
> > Muppet.instance_eval("remove_method #{m.inspect}")}
> > 
> > Note 1. kermit.methods will return all methods inherited or not.
> > Note 2. remove method is a class method, so it's evaluated in the
> > context of the class object, hence Muppet.instance_eval
> > Note 3: you could alternatively pass symbols for the method names
> > 
> > kermit.class.instance_methods(false).each{|m|
> > Muppet.instance_eval("remove_method :#{m}")}
> 
>  Muahahaha! That was awesome! Soon the world will quake in fear when I
>  put this information to use!
> 
>  But why doesn't this work?
> 
> >> kermit.methods(false).each do |m|
>  ?>     Object.instance_eval("remove_method :#{m}")
> >>   end
>  => []
> >> kermit.methods
>  => 53

kermit.methods(false) will give you kermit's singleton methods:

irb(main):019:0>  s = ""
=>  ""
irb(main):020:0>  def s.x; end
=>  nil
irb(main):021:0>  s.methods(false)
=>  ["x"]

Also, you can't remove these methods from Object because they're not
defined there.  You'd have to do:

   class << s
     remove_method(:x)
   end


David

-- 
* Books:
   RAILS ROUTING (new! http://safari.awprofessional.com/9780321509246)
   RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
     & consulting:  Ruby Power and Light, LLC (http://www.rubypal.com)
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