[英]Ruby on Rails - Is params a method or a hash?
So, I am trying out the Getting Started section of the Ruby on Rails guides here . 所以,我想出来的Ruby on Rails的的入门部分指导这里 。
I did not understand a line in this tutorial. 我不明白本教程中的一行 。 Quoting it:
引用它:
The params method is the object which represents the parameters (or fields) coming in from the form.
params方法是表示从表单进入的参数(或字段)的对象。
I do have some previous experience in rails, and I always assumed params
is a hash. 我确实有一些以前的rails经验,我总是认为
params
是一个哈希。 But here they call it a method which is an object
. 但在这里,他们称之为
method which is an object
的method which is an object
。
Is params
a method
or a hash
? params
是method
还是hash
? Also, in ruby, are methods also objects? 另外,在ruby中,方法也是对象吗?
params
is a method that returns an ActionController::Parameters
object . params
是返回的方法 ActionController::Parameters
对象 。 Think of it something like this: 想想它是这样的:
def params ActionController::Parameters.new(...) end
Example (somewhere in your controller or view) : 示例(控制器或视图中的某个位置) :
puts params #=> <ActionController::Parameters ...> puts params.is_a? Object #=> true
A method in Ruby always return a value (note: nil
is also a value ) unless that method is not defined. Ruby中的方法总是返回一个值 (注意:
nil
也是一个值 ),除非没有定义该方法 。 Keep in mind though that params
is already defined by Rails even if you do not see it in your code) 请记住,即使你的代码中没有看到它已经由Rails定义了
params
)
Every "returned value" of a method in Ruby is an object . Ruby中方法的每个“返回值”都是一个对象 。 Even
nil
value is a NilClass
object . 甚至
nil
值也是NilClass
对象 。 Integers, Strings, Arrays, and Hashes are also objects . 整数,字符串,数组和哈希也是对象 。 Why?
为什么? Because eveeeeery thing inherits/starts of from the
Object
class . 因为eveeeeery事物从
Object
类继承/启动。
If < Rails 5.1: 如果<Rails 5.1:
ActionController::Parameters
inherits from Hash
class , which means that you can use all Hash
methods ( see Hash methods here ). ActionController::Parameters
继承自Hash
类 ,这意味着您可以使用所有Hash
方法 ( 请参阅此处的Hash方法 )。
Example: 例:
params.sort ... params.each ... params.has_key?(:controller)
But now at >= Rails 5.1: 但现在在> = Rails 5.1:
ActionController::Parameters
NO LONGER inherits from Hash
class , so you would think that you can no longer use methods such as the above code like .sort
, or .has_key?
ActionController::Parameters
NO LONGER继承自Hash
类 ,所以你会认为你不能再使用像.sort
或.has_key?
这样的代码之类的方法了.has_key?
, but you still can! ActionController::Parameters
defines its own custom methods that "look like" methods from a Hash
. ActionController::Parameters
定义了自己的自定义方法,这些方法来自Hash
“看起来像”方法。
ActionController::Parameters
. ActionController::Parameters
中重新定义。 Feel free to compare the methods HERE and HERE , in particular sort
method which is Hash
method was not redefined in ActionController::Parameters
, so you can't do params.sort
anymore in Rails >= 5.1. ActionController::Parameters
没有重新定义Hash
方法的sort
方法,所以你不能在Rails> = 5.1中再做params.sort
。 The description is a little bit truncated. 描述有点截断。 To be precise, it should be read as:
确切地说,它应该被理解为:
The return value of the
params
method is [an] object ...params
方法的返回值是[an]对象......
A method is not an object, but when you execute a method, it always returns an object. 方法不是对象,但是当您执行方法时,它总是返回一个对象。 In this case,
params
is a method, not an object, but has a return value, which is an object. 在这种情况下,
params
是一个方法,而不是一个对象,但有一个返回值,它是一个对象。
In older versions of Rails, the return value of params
used to be a hash, but now, it isn't a hash. 在旧版本的Rails中,
params
的返回值曾经是一个哈希值,但现在,它不是一个哈希值。
params
is a method which returns an object - instance of ActionController::Parameters
. params
是一个返回对象的方法 - ActionController::Parameters
实例。
It has methods that make it behave much like a hash, but it is a bit more complex than a hash. 它有一些方法使它的行为很像哈希,但它比哈希复杂一点。
Also, in Ruby, are methods also objects?
另外,在Ruby中,方法也是对象吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.