简体   繁体   English

对“ respond_to”和“ respond_to”感到困惑?

[英]Confused about 'respond_to' vs 'respond_to?'

I am learning Rails with railstutorial.org, and I am confused about something: in this chapter the author tells us to do some testing in the console with the respond_to? 我正在用railstutorial.org学习Rails,并且对某些事情感到困惑:在本章中 ,作者告诉我们在控制台中使用react_to进行一些测试respond_to? method on a User object, and it works ok. 用户对象上的方法,它可以正常工作。 But later, when we write the test for the :encrypted_password attribute, he uses respond_to . 但是后来,当我们为:encrypted_password属性编写测试时,他使用了respond_to

Out of curiosity, I tried respond_to in the console, for a User object, and I get an error saying the method doesnt exist. 出于好奇,我在控制台中尝试了User对象的respond_to ,但收到一条错误消息,指出该方法不存在。 Alas, if I try to write the test using respond_to? ,如果我尝试使用respond_to?编写测试respond_to? instead of respond_to , the test doesnt run. 而不是respond_to ,测试不会运行。

Could someone explain me the difference, and why does the test only run with respond_to ? 有人可以解释一下两者之间的区别,为什么测试仅使用respond_to运行?

Ruby treats ? 红宝石请客? and ! ! as actual characters in a method name. 作为方法名称中的实际字符。 respond_to and respond_to? respond_torespond_to? are different. 是不同的。 ? indicates that this should respond with a true or false (by convention; this is not a requirement). 表示这应该以对或错响应(按照惯例;这不是必需的)。 Specifically: 特别:

respond_to? is a Ruby method for detecting whether the class has a particular method on it. 是用于检测类是否具有特定方法的Ruby方法 For example, 例如,

@user.respond_to?('eat_food')

would return true if the User class has an eat_food method on it. 如果User类上具有eat_food方法,则将返回true。

respond_to is a Rails method for responding to particular request types. respond_to是用于响应特定请求类型的Rails方法 For example: 例如:

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 RSpec method should interacting with RSpec's respond_to method. 但是,在您提供的RailsTutorial链接中,您看到RSpec方法should与RSpec的respond_to方法进行交互。 This wouldn't be available in your console, unless you run rails console test . 除非您运行rails console test ,否则它在您的控制台中将不可用。

respond_to? is a Boolean evaluation. 是布尔值评估。 The respond_to is used (normally) for determining the display information. 通常, respond_to用于确定显示信息。 More information here . 更多信息在这里 The respond_to? respond_to? checks to see if a method exists and returns true if it does and false if it doesn't. 检查方法是否存在,如果存在,则返回true,否则返回false。

The test uses convenient helpers to be more user friendly. 该测试使用方便的助手来使用户更加友好。

Ruby is Ruby so using the good old respond_to? Ruby是Ruby,因此使用好的旧版respond_to? would work if you call it this way: 如果以这种方式调用它将起作用:

 @user.respond_to?(:encrypted_password).should be_true

There is another respond_to used in controllers but still nothing to do with those you already met. 控制器中还有另一个respond_to ,但与您已经遇到的那些无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM