2

在玩了一下 Ruby 时,我编写了以下代码:

class A
end

A.singleton_class.instance_eval do
    undef_method :new
end

# or
# class << B
#   undef_method :new
# end

A.new

> NoMethodError: undefined method `new' for A:Class
>         from (irb):8
>         from /home/mmsequeira/.rvm/rubies/ruby-1.9.3-p327/bin/irb:16:in `<main>'

这很酷。但是我怎么知道给定类中哪些方法是未定义的呢?

4

2 回答 2

3

你不能默认。取消定义方法会将其从存在中删除。但是,您可以在它们被删除时记录它们。这可以通过方法挂钩来完成,以捕获所有内容并避免丑陋的别名方法链接:

class Module
  def method_undefined name
    (@undefined_methods ||= []) << name
  end

  def singleton_method_undefined name
    (@undefined_methods ||= []) << name
  end

  def undefined_methods
    @undefined_methods || []
  end
end

undef_method这将通过or捕获未定义的方法undef

class C
  def foo; end
  def bar; end

  undef foo
  undef_method :bar
end

C.undefined_methods  #=> [:foo, :bar]
C.singleton_class.instance_eval { undef new }
C.singleton_class.undefined_methods  #=> [:new]

当然,您必须在 Module 中包含钩子方法,然后才能捕获任何内容。

于 2012-11-25T17:13:54.963 回答
1

也许你需要重新定义Module#undef_method.

class Module
  alias original_undef_method :undef_method
  @@undef_methods = {}
  def undef_method *methods
    methods.each{|method| @@undef_methods[[self, method]] ||= true}
    original_undef_method(*methods)
  end
  def self.undef_methods; @@undef_methods.keys end
end

然后,你得到:

class A
end
A.singleton_class.instance_eval do
    undef_method :new
end
Module.undef_methods
# => [[#<Class:A>, :new]]
于 2012-11-25T16:48:40.203 回答