tasteslikelemons
tasteslikelemons

Reputation: 397

Rails params "method" isn't a method, is it?

In the Rails documentation, the following example is given as a way to display what the server receives from a POST request:

def create
  render plain: params[:article].inspect
end 

In the subsequent description, the text states

The params method is the object which represents the parameters (or fields) coming in from the form. The params method returns an ActiveSupport::HashWithIndifferentAccess object.

While I understand that all methods are objects, I don't understand how it's correct to refer to the params object as a method. Specifically, the phrase "returns an ActiveSupport::HashWithIndifferentAccess object" suggests to me that there are two calls going on--what in python might look like:

params().__getitem__('article')

but I don't think that's what's actually going on.

The conversation around those lines also refers to params as a method, so I'm starting to think I must be missing something.

Upvotes: 1

Views: 293

Answers (3)

Jordan Allan
Jordan Allan

Reputation: 4486

params is a method defined in ActionController::Metal which returns the request.parameters object.

https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal.rb#L140

Upvotes: 1

spickermann
spickermann

Reputation: 106932

The params method is a method, returns a hash (which holds some details about parameters send to the app). Simplified it looks like this:

def fake_params
  { :controller => 'foo', :action => 'bar' }
end

You can call another method directly on the returned hash like this:

fake_params[:action]    #=> 'bar'

Upvotes: 2

Jörg W Mittag
Jörg W Mittag

Reputation: 369468

I'm new to Ruby, and while I understand that all methods are objects,

No, they aren't. Methods belong to objects (more precisely: methods are defined in modules, and executed in the context of objects), but they are not, by themselves, objects. (It is, however, possible to obtain a reflective proxy which represents the concept of a method by calling the method method, which returns a Method object.)

I don't understand how it's correct to refer to the params object as a method.

Because it is a method. Not an object.

What else would it be? Syntactically, it's obvious that it can only be one of three things: a keyword, a variable, or a method call.

It can't be a keyword, because Rails is just a Ruby library, and Ruby libraries can't change the syntax of the language (e.g. add or remove keywords). It can't be a variable, because in order for it to be parsed as a variable, the parser would need to have seen an assignment to it within the same block.

Ergo, the only thing it can possibly be, is a method call. You don't even need to know anything about Rails to know this. It's just basic Ruby syntax 101.

Specifically, the phrase "returns an ActiveSupport::HashWithIndifferentAccess object" suggests to me that there are two calls going on--what in python might look like:

params().__getitem__('article')

but I don't think that's what's actually going on.

That is exactly what is going on. You call the method params and then you call the method [] on the object that is returned by calling the method params.

This is in no way different from foo.bar: you call foo, then call bar on the return value of foo.

Upvotes: 4

Related Questions