Forum: Ruby Remove all class methods via remove_method

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.
Bb0f5caf498c99d2300692c1b3089931?d=identicon&s=25 dodeantn (Guest)
on 2009-02-01 20:35
(Received via mailing list)
Hi,

how do I remove all my class methods like in the following example? --

class Parent
class << self
  def class_stuff
    puts "Stuff from my class"
  end
end
end

class Child < Parent
  # remove all the class methods;
  # should be done automatically with "...methods.each { |m|
remove_method(m) }"-like construct
end

Many thanks in advance.
391f9b787cdc12aa2c179713f5103e3a?d=identicon&s=25 Ilan Berci (iberci)
on 2009-02-02 21:21
dodeantn,
This will do it but I would prefer a better way..

class Parent
  (class << self; self; end).send(:remove_method, :class_stuff)
end

Please remember that with great power comes great responsibility.. :)
messing with class methods is a real pain in 1.8 but maybe I am missing
the boat..

hth

ilan





dodeantn wrote:
> Hi,
>
> how do I remove all my class methods like in the following example? --
>
> class Parent
> class << self
>   def class_stuff
>     puts "Stuff from my class"
>   end
> end
> end
>
> class Child < Parent
>   # remove all the class methods;
>   # should be done automatically with "...methods.each { |m|
> remove_method(m) }"-like construct
> end
>
> Many thanks in advance.
This topic is locked and can not be replied to.