railstutorial 调试信息来自哪里 [英] railstutorial where is debug information coming from
问题描述
在第 7 章中,我使用
In chapter 7 I'm getting the following output with
--- !ruby/hash-with-ivars:ActionController::Parameters
elements:
controller: static_pages
action: home
ivars:
:@permitted: false
有人可以解释带有 ivars 的 hash 来自哪里以及 ivars 是什么::@permitted: false 是什么意思?
could someone explain where the hash-with-ivars is coming from and what ivars: :@permitted: false means?
推荐答案
有趣的问题!我在 Rails 项目的所有库源中搜索了 'hash-with-ivars'
,但只有一个地方出现:psych
ruby 库,用于(反)序列化任意对象到 YAML.具体来说,这些是阅读的源代码链接和编写这个 YAML 结构.
Interesting question! I searched all library sources of a rails project for 'hash-with-ivars'
and only a single place came up: the psych
ruby library for (de-)serializing arbitrary objects to and from YAML. Specifically, these are links to the source code for reading and writing this YAML structure.
在 Rails 教程的 第 7 章中,此输出作为 debug(params)
命令,您被指示放入模板.debug
命令显然调用了 psych
库以显示对象的可读表示(在本例中为 params
).
In Chapter 7 of the Rails Tutorial, this output comes out as the output of the debug(params)
command that you are instructed to put in a template. The debug
command apparently calls the psych
library to show a readable representation of the object (params
in this case).
现在,params
——用于保存从 URL 或表单传递的参数的通用 Rails 数据结构——是一个行为类似于 Hash
但不是纯哈希的对象:它是ActionController::Parameters
类的一个实例,它是Hash
的子类,让我们看看类定义:
Now, params
- the universal Rails data structure to hold parameters passed from URLs or forms - is an object that behaves like a Hash
but is not pure hash: it is an instance of class ActionController::Parameters
which is a subclass of Hash
, let's see the class definition:
module ActionController
# ...
class Parameters < ActiveSupport::HashWithIndifferentAccess
# ...
end
end
而 HashWithIndifferentAccess
是 Hash
的直接子类.
while HashWithIndifferentAccess
is a direct subclass of Hash
.
作为 Hash
的子类,params
对象可以保存除哈希本身之外的其他数据,而这正是 psych
在尝试时实际支持的以可读的形式打印对象.除了打印所有哈希元素(在 elements
键下),它还尝试列出对象的所有实例变量并将其打印在 ivars
下> 键.
As a subclass of Hash
, the params
object can hold other data besides the hash itself and this is what psych
actually supports when trying to print the object in a readable form. Besides printing all the hash elements (under the elements
key), it tries to also list all instance variables of the object and prints it under the ivars
key.
所以,总而言之,这个调试打印只是说被调试的对象是 ActionController::Parameters
类的一个实例,它是 Hash
的子类,除了它的散列元素之外,还定义了一个 @permitted
实例变量,它当前设置为 false
.顺便说一下,controller
和 action
这两个元素是 Rails 内部用于路由的参数.
So, all in all, this debug print simply says that the object debugged is an instance of the ActionController::Parameters
class, which is a subclass of Hash
, that besides it's hash elements has also a @permitted
instance variable defined and it is currently set to false
. The two elements, by the way, the controller
and action
are parameters used internally by Rails for routing.
当你再次查看类的源代码时,你确实会在构造函数中找到@permitted
变量:
When you look into the source of the class again, you will indeed find the @permitted
variable right in the constructor:
class Parameters < ActiveSupport::HashWithIndifferentAccess
# ...
def initialize(attributes = nil)
super(attributes)
@permitted = self.class.permit_all_parameters
end
end
最后,从文档我们可以得出结论,@permitted
变量保存 params 权限的状态.IE.在使用 permit
方法允许参数后,它被设置为 true
:
Finally, from the documentation we can conclude that the @permitted
variable holds the state of the params permission. I.e. it is set to true
after the params are permitted by using the permit
method:
permitted = params.require(:person).permit(:name, :age)
permitted.permitted? # this prints out the @permitted instance variable
# => true
更新:为什么 RailsTutorial 的调试输出不同
RailsTutorial 的调试输出略有不同 - 它不打印 ivar
s.为什么?这是因为序列化 hash-with-ivars
的功能已添加到 psych
gem 中的 2.0.9 版.psych
gem 现在是 Ruby 标准库的一部分,它的这个特定版本添加到 stdlib 2.3.0 preview1 版本.
The debug output at the RailsTutorial differs a bit - it does not print ivar
s. Why? It is because the feature for serializing hash-with-ivars
was added to the psych
gem in its version 2.0.9. The psych
gem is now a part of the Ruby standard library and this particular version of it has been added to the stdlib 2.3.0 preview1 version.
所以,神秘的不同输出有一个简单的解释:RailsTutorial 作者在写这本书时很可能使用了 ruby 2.2 或更早版本,而这个 ruby 版本还没有在 Hash
调试输出中显示实例变量.实际上,教程中有提示建议作者使用了 ruby 2.1.5.
So, the mysteriously different output has a simple explanation: the RailsTutorial author most probably used ruby 2.2 or earlier when writing the book and this ruby version did not display instance variables in the Hash
debug output yet. Actually, there are hints in the tutorial that suggest that the author used ruby 2.1.5.
这篇关于railstutorial 调试信息来自哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!