- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
查看 Rails 代码库有时会出现 respond_to_missing?调用 super 有时不调用。是否存在不应从 respond_to_missing 调用 super 的情况?
最佳答案
这取决于类的实现和您希望 #respond_to_missing?
的行为。查看 ActiveSupport::TimeWithZone
,它是 Time
的代理包装器。它试图模仿它,让您误以为它是 Time
的一个实例。例如,TimeWithZone#is_a?
在经过 Time
时会响应 true
。
# Say we're a Time to thwart type checking.
def is_a?(klass)
klass == ::Time || super
end
alias_method :kind_of?, :is_a?
respond_to_missing?
应该捕获将被 method_missing
捕获的情况,因此您必须查看这两种方法。 TimeWithZone#method_missing
将缺失的方法委托(delegate)给 Time
而不是 super
。
def method_missing(sym, *args, &block)
wrap_with_time_zone time.__send__(sym, *args, &block)
rescue NoMethodError => e
raise e, e.message.sub(time.inspect, inspect), e.backtrace
end
因此,将 respond_to_missing?
也委托(delegate)给 Time
是有道理的。
# Ensure proxy class responds to all methods that underlying time instance
# responds to.
def respond_to_missing?(sym, include_priv)
return false if sym.to_sym == :acts_like_date?
time.respond_to?(sym, include_priv)
end
关于ruby-on-rails - ruby respond_to_missing?叫 super 还是不叫?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45465607/
使用 method_missing 时在 Ruby 中,它是 almost always a good idea定义 respond_to_missing? respond_to_missing?接受
与定义 respond_to? 相比,定义 respond_to_missing? 有什么意义?如果为某些类重新定义 respond_to? 会出什么问题? 最佳答案 没有respond_to_mis
我对何时使用这些方法感到困惑。来自 respond_to? 文档: Returns true if obj responds to the given method. Private methods
Rubocop 给我以下罪行 lib/daru/vector.rb:1182:5: C: Style/MethodMissing: When using method_missing, define
查看 Rails 代码库有时会出现 respond_to_missing?调用 super 有时不调用。是否存在不应从 respond_to_missing 调用 super 的情况? 最佳答案 这取
我是一名优秀的程序员,十分优秀!