7

So, I am trying out the Getting Started section of the Ruby on Rails guides here.

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.

I do have some previous experience in rails, and I always assumed params is a hash. But here they call it a method which is an object.

Is params a method or a hash? Also, in ruby, are methods also objects?

mridula
  • 3,203
  • 3
  • 32
  • 55

3 Answers3

16
  • params is a method that returns an ActionController::Parameters object. 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. Keep in mind though that params is already defined by Rails even if you do not see it in your code)

  • Every "returned value" of a method in Ruby is an object. Even nil value is a NilClass object. Integers, Strings, Arrays, and Hashes are also objects. Why? Because eveeeeery thing inherits/starts of from the Object class.

  • If < Rails 5.1:

    • ActionController::Parameters inherits from Hash class, which means that you can use all Hash methods (see Hash methods here).

      Example:

      params.sort ...
      params.each ...
      params.has_key?(:controller)
      
  • But now at >= 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?, but you still can! because ActionController::Parameters defines its own custom methods that "look like" methods from a Hash.
      • NOTE: Not all Hash methods are redefined in 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.
Jay-Ar Polidario
  • 6,463
  • 14
  • 28
7
  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 ...

    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.

  2. In older versions of Rails, the return value of params used to be a hash, but now, it isn't a hash.

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
sawa
  • 165,429
  • 45
  • 277
  • 381
  • 2
    @EricDuminil Hey, don't try to confuse beginners. An instance of `Method` class is created by a method named `method`, and is an object. It is distinct from a method. – sawa May 19 '17 at 12:00
  • You're right. I've read more about it in an answer by our [Ruby god](http://stackoverflow.com/a/4294660/6419007), and deleted my previous comment. – Eric Duminil May 19 '17 at 12:06
  • 2
    Eric's previous comment made me run `puts method(:params).source_location`. It led me to https://github.com/rails/rails/blob/4b969ea417f097d217ff1f662d2fe8c406d21b99/actionpack/lib/action_controller/metal/strong_parameters.rb where I literally saw the `params` method ;) – mridula May 19 '17 at 12:12
3

params is a method which returns an object - instance of 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?

Check this thread.

Community
  • 1
  • 1
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145