Ruby-on-rails 对“respond_to”与“respond_to”感到困惑?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6849722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Confused about 'respond_to' vs 'respond_to?'
提问by agente_secreto
I am learning Rails with railstutorial.org, and I am confused about something:
in this chapterthe author tells us to do some testing in the console with the respond_to?method on a User object, and it works ok. But later, when we write the test for the :encrypted_passwordattribute, he uses respond_to.
我正在通过 railstutorial.org 学习 Rails,我对一些事情感到困惑:在本章中,作者告诉我们在控制台中使用respond_to?User 对象上的方法进行一些测试,并且它工作正常。但是后来,当我们为该:encrypted_password属性编写测试时,他使用respond_to.
Out of curiosity, I tried respond_toin the console, for a User object, and I get an error saying the method doesnt exist. Alas, if I try to write the test using respond_to?instead of respond_to, the test doesnt run.
出于好奇,我respond_to在控制台中尝试使用 User 对象,但收到错误消息,指出该方法不存在。唉,如果我尝试使用respond_to?而不是编写测试respond_to,则测试不会运行。
Could someone explain me the difference, and why does the test only run with respond_to?
有人可以解释我的区别,为什么测试只运行respond_to?
回答by Tim Sullivan
Ruby treats ?and !as actual characters in a method name. respond_toand respond_to?are different. ?indicates that this shouldrespond with a true or false (by convention; this is not a requirement). Specifically:
Ruby 将?和!视为方法名称中的实际字符。respond_to并且respond_to?是不同的。?表示这应该以 true 或 false 响应(按照惯例;这不是必需的)。具体来说:
respond_to?is a Rubymethodfor detecting whether the class has a particular method on it. For example,
respond_to?是一种Ruby方法,用于检测该类是否具有特定方法。例如,
@user.respond_to?('eat_food')
would return true if the Userclass has an eat_foodmethod on it.
如果User该类有一个eat_food方法,则返回 true 。
respond_tois a Railsmethodfor responding to particular request types. For example:
respond_to是用于响应特定请求类型的Rails方法。例如:
def index
@people = Person.find(:all)
respond_to do |format|
format.html
format.xml { render :xml => @people.to_xml }
end
end
However, in the RailsTutorial link you've provided, you're seeing an RSpecmethod shouldinteracting with RSpec's respond_tomethod. This wouldn't be available in your console, unless you run rails console test.
但是,在您提供的 RailsTutorial 链接中,您看到RSpec方法should与 RSpec 的respond_to方法交互。这在您的控制台中不可用,除非您运行rails console test.
回答by Micharch54
respond_to?is a Boolean evaluation. The respond_tois used (normally) for determining the display information. More information here. The respond_to?checks to see if a method exists and returns true if it does and false if it doesn't.
respond_to?是布尔评估。的respond_to是,用于确定所述显示信息中使用(一般)。 更多信息在这里。该respond_to?检查是否如果它和虚假如果没有一种方法存在,返回true。
回答by apneadiving
The test uses convenient helpers to be more user friendly.
该测试使用方便的助手以更加用户友好。
Ruby is Ruby so using the good old respond_to?would work if you call it this way:
Ruby 是 Ruby,所以respond_to?如果你这样称呼它,使用旧的就可以了:
@user.respond_to?(:encrypted_password).should be_true
There is another respond_toused in controllers but still nothing to do with those you already met.
还有另一个respond_to用于控制器,但仍然与您已经遇到的那些无关。

