简体   繁体   English

Ruby on Rails - params是方法还是哈希?

[英]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 objectmethod which is an object

Is params a method or a hash ? paramsmethod还是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! ,但你还可以! because ActionController::Parameters defines its own custom methods that "look like" methods from a Hash . 因为ActionController::Parameters定义了自己的自定义方法,这些方法来自Hash “看起来像”方法。
      • NOTE: Not all Hash methods are redefined in ActionController::Parameters . 注意:并非所有Hash方法都在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. 随意比较HEREHERE的方法,特别是在ActionController::Parameters没有重新定义Hash方法的sort方法,所以你不能在Rails> = 5.1中再做params.sort
  1. 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是一个方法,而不是一个对象,但有一个返回值,它是一个对象。

  2. 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中,方法也是对象吗?

Check this thread . 检查这个帖子

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

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