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?
params is a method on the ActionController::StrongParameter class. While params appears to be a hash, it is actually an instance of the ActionController::Parameters class.
Specifically, params refers to the parameters being passed to the controller via a GET or POST request. In a GET request, params get passed to the controller from the URL in the user's browser. For example, if our app's controller looked like. def show. @person = Person.find(params[:name])
Params is short for the word parameter. A parameter is a key-value pair that is encoded in a HTTP request. There are three kinds of params: user supplied parameters , routing parameters , and default parameters .
Note that parameter values are always strings; Rails does not attempt to guess or cast the type.
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.
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.The description is a little bit truncated. To be precise, it should be read as:
The return value of the
paramsmethod 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.
In older versions of Rails, the return value of params used to be a hash, but now, it isn't a hash.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With