Remove/undef a class method

后端 未结 6 1370
滥情空心
滥情空心 2020-12-13 03:59

You can dynamically define a class method for a class like so:

class Foo
end

bar = %q{def bar() \"bar!\" end}
Foo.instance_eval(bar)

But h

6条回答
  •  感动是毒
    2020-12-13 04:28

    This also works for me (not sure if there are differences between undef and remove_method):

    class Foo
    end
    
    Foo.instance_eval do
      def color
        "green"
      end
    end
    
    Foo.color # => "green"
    
    Foo.instance_eval { undef :color }
    
    Foo.color # => NoMethodError: undefined method `color' for Foo:Class
    

提交回复
热议问题