或者,我该如何做到完美?如何在Ruby中评估尚未声明的类?
这是我想出了迄今:
# A double that stands in for a yet-to-be-defined class. Otherwise
# known as "lazy evaluation."
#
# Idea lifted from:
# http://github.com/soveran/ohm/
class Double < BasicObject
def initialize(name)
@name = name
end
def to_s
@name.to_s
end
alias_method :inspect, :to_s
def method_missing(mth, *args, &block)
@unwrapped ? super : @unwrapped = true
::Kernel.const_get(@name).send(mth, *args, &block)
ensure
@unwrapped = false
end; private :method_missing
end
这工作:
foo = Double(:Foo) # Now we can safely pass around Foo without
# having initialised it.
foo.class # Uninitialised constant
# That's expected because Foo doesn't exist yet!
class Foo; end # So there, we shoo it into existence.
foo.class # Foo # foo indeed is Foo. The sleight of hand of works.
这是我不能去上班:
inst = Foo.new
inst.is_a? Foo # true, of course
inst.is_a? foo # TypeError: class or module required
为什么在最后一行中,Foo的双打不会站出来吗?
是的,我想这是一个隐藏在双层背后的类的想法不起作用的时刻。只需要学会忍受它。 –