- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我对何时使用这些方法感到困惑。来自 respond_to?
文档:
Returns true if obj responds to the given method. Private methods are included in the search only if the optional second parameter evaluates to true.
If the method is not implemented, as Process.fork on Windows, File.lchmod on GNU/Linux, etc., false is returned.
If the method is not defined, respond_to_missing? method is called and the result is returned.
respond_to_missing?
:
Hook method to return whether the obj can respond to id method or not.
See #respond_to?.
这两种方法都有两个参数。
这两种方法似乎是同一件事(检查某个对象是否响应给定方法)那么为什么我们应该同时使用(拥有)这两种方法?
定义“resond_to_missing?”让您能够采取方法:
class A
def method_missing name, *args, &block
if name == :meth1
puts 'YES!'
else
raise NoMethodError
end
end
def respond_to_missing? name, flag = true
if name == :meth1
true
else
false
end
end
end
[65] pry(main)> A.new.method :meth1
# => #<Method: A#meth1>
为什么 respond_to?
不能这样做?
respond_to?
检查方法是否在:
respond_to_missing?
检查方法是否:
method_missing
定义:通过一系列可能的方法:
def method_missing name, *args, &block
arr = [:a, :b, :c]
if arr.include? name
puts name
else
raise NoMethodError
end
end
委托(delegate)给不同的对象:
class A
def initialize name
@str = String name
end
def method_missing name, *args, &block
@str.send name, *args, &block
end
end
2。我不知道的其他方式。
从 1.9.3 开始(我记得很公平)仅定义 respond_to_missing?
但仅使用 respond_to?
我说的对吗?我错过了什么吗?更正所有错误的内容和/或回答此问题中提出的问题。
最佳答案
respond_to_missing?
应该在您使用方法缺失技术提供其他方法时更新。这将使 Ruby 解释器更好地理解新方法的存在。
事实上,如果不使用respond_to_missing?
,您将无法使用method
获取方法。 .
Marc-André posted a great article关于 respond_to_missing?
。
In order for respond_to? to return true, one can specialize it, as follows:
class StereoPlayer
# def method_missing ...
# ...
# end
def respond_to?(method, *)
method.to_s =~ /play_(\w+)/ || super
end
end
p.respond_to? :play_some_Beethoven # => trueThis is better, but it still doesn’t make play_some_Beethoven behave exactly like a method. Indeed:
p.method :play_some_Beethoven
# => NameError: undefined method `play_some_Beethoven'
# for class `StereoPlayer'Ruby 1.9.2 introduces
respond_to_missing?
that provides for a clean solution to the problem. Instead of specializingrespond_to?
one specializesrespond_to_missing?
. Here’s a full example:class StereoPlayer
# def method_missing ...
# ...
# end
def respond_to_missing?(method, *)
method =~ /play_(\w+)/ || super
end
end
p = StereoPlayer.new
p.play_some_Beethoven # => "Here's some_Beethoven"
p.respond_to? :play_some_Beethoven # => true
m = p.method(:play_some_Beethoven) # => #<Method: StereoPlayer#play_some_Beethoven>
# m acts like any other method:
m.call # => "Here's some_Beethoven"
m == p.method(:play_some_Beethoven) # => true
m.name # => :play_some_Beethoven
StereoPlayer.send :define_method, :ludwig, m
p.ludwig # => "Here's some_Beethoven"
另见 Always Define respond_to_missing? When Overriding method_missing .
关于ruby - respond_to 和 respond_to_missing 有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20731167/
我正在通过railstutorial.org学习Rails,但我对一些事情感到困惑:在 this chapter作者告诉我们在控制台中使用 User 对象的 respond_to? 方法进行一些测试,
我可以在 irb 的主要对象中使用 respond_to?(): irb(main):001:0> def foo irb(main):002:1> "hi" irb(main):003:1> e
在所有其他方法中,它与 format.html 一起工作正常,但在这个与 format.js 的方法中,它更新 View 但可见 url 保持不变 respond_to do |format| f
我目前正在构建一个需要远程表单的应用程序,我发现我从来没有真正理解rails respond_to 的工作原理。 例如,我在 View 中有以下代码 true %> 我有一个名为 _home.j
我试过: 1.respond_to? "clone" # => true 1.clone # => TypeError: can't clone Fixnum
如果您有一个要测试其存在的属性列表,并且您有以下内容: before do @attr = Employee.new(name: "One", ema
我有一个模块定义了一个方法,如果它还没有被定义的话。 ActiveRecord 的属性就是这种情况,因为它们的 getter 和 setter 未定义为方法。 module B def creat
编辑:我使用的是 Ruby 2.0.0 版 我在文件 example.rb 中有以下代码 def say_hello puts 'hi' end puts respond_to?(:say_hel
与定义 respond_to? 相比,定义 respond_to_missing? 有什么意义?如果为某些类重新定义 respond_to? 会出什么问题? 最佳答案 没有respond_to_mis
假设我有一个 ArtcilesController使用如下创建操作。 def create @article = Article.new(params[:article]) respond_t
如果我想检查是否定义了具有给定名称的方法,最好使用respond_to?或defined?? 从效率的角度来看,使用defined?可能存在争议,因为defined?是内置关键字,而respond_t
我有一个使用 form_for 和 remote: true 设置的表单。 正如预期的那样,vocab Controller 中的相应更新操作将请求作为 JS 处理: Processing by V
我的 cart_controller 中有几个 Action 。我在/carts 中有 2 个 View :payment.html.erb 和 pay.html.erb def pay suc
我的 Rails 应用程序中创建 Controller 中的 respond_to block 在成功保存时没有重定向...我确信这是一个简单的解决方案,但我对 Rails 缺乏经验,这是我第一次遇到
我的 cart_controller 中有几个 Action 。我在/carts 中有 2 个 View :payment.html.erb 和 pay.html.erb def pay suc
我试图解决这个问题,但我需要帮助! 我正在使用rails 3和jquery,我包含rails.js并使用gem'jquery-rails'来创建它 - 但仍然;使用 $.ajax({url: self
我有一个评级 Controller ,其创建操作通过 Javascript 调用。我的评级文件夹中有一个 create.js 文件。我想为了调用 create.js 文件你需要这样的东西 respon
我的 Controller 中有这个方法: def index @hedge_fund_index_matrix = HedgeFundIndices.matrix @hedge_fu
最近我一直在尝试Rails,并且开始喜欢上respond_to。 在 PHP 中是否可以做这样的事情?,响应不同类型的请求。因此,即使禁用了 javascript,也很容易实现替代方法。 最佳答案 您
我目前正在尝试为收费表单编写功能测试,该表单通过 AJAX(jQuery) 加载到页面上。它从 charge_form 操作加载表单,该操作返回 Consult_form.js.erb View 。
我是一名优秀的程序员,十分优秀!