Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where did this Ruby parameter convention come from?

There's a piece of Ruby middleware used by Rails and other frameworks to the parse the parameters you've sent to the server into a nested Hash object.

If you send these parameters to the server:

person[id] = 1
person[name] = Joe Blow
person[email] = [email protected]
person[address][street_address] = 123 Somewhere St.
person[address][city] = Chicago
person[address][zip] = 12345
person[other_field][] = 1
person[other_field][] = 2
person[other_field][] = 3

They get parsed to this:

{
  :person => {
    :id => "1",
    :name => "Joe Blow",
    :email => "[email protected]",
    :address => {
      :street_address => "123 Somewhere St.",
      :city => "Chicago",
      :state => "IL",
      :zip => "12345"
    },
    :other_field => [ 1, 2, 3 ]
  }
}

I believe this is supported by PHP as well. Can anybody tell me what this convention is called, where it came from, and what other languages support it? (Perl, Python, etc.)

like image 895
Adam Lassek Avatar asked Sep 28 '11 04:09

Adam Lassek


People also ask

What is Ruby parameter?

What is a parameter? Parameters in ruby are variables that are defined in method definition and which represent the ability of a method to accept arguments. So, if we will not have the appropriate parameters, then we will not be able to pass arguments to a method that will contain the data we need.

What is params [: id in Rails?

params[:id] is meant to be the string that uniquely identifies a (RESTful) resource within your Rails application. It is found in the URL after the resource's name.

Are rails params strings?

Note that parameter values are always strings; Rails does not attempt to guess or cast the type.


2 Answers

Field research

I'm trying to find out if there's a name for this convention, but I can't find it yet.

Ruby

For what it's worth, the piece of middleware that does this in Ruby is Rack::Utils. See the source on Github.

There is some more information on the subject in the Ruby on Rails Guides.

And here is an interesting ticket about the code being moved from Rails to the Rack middleware.

PHP

I've done some digging in the PHP source, and it seems that all the magic there happens in the main/php_variables.c source file. The SAPI code calls the php_std_post_handler method defined here. This eventually calls the php_register_variable_ex method, which is 185 lines of complex string-parsing in C (I must admit that C isn't my forte).

The only name I could find here was the php_std_post_handler, the PHP standard POST handler.

Python

In Python, this format isn't supported by default. See this question here on stackoverflow.com on the subject.

Perl

The CGI library in Perl doesn't support this format either. It does give easy access to single or multiple values, but not nested values as in your example. See the documentation on feching the value or values of a single named parameter and fetching the parameter list as a hash.

Java

Check out the heated debate on the subject of query parameter parsing in this question. Java doesn't parse this 'nested format' of POST parameters into a data structure by default.

Conclusion

I've looked into this, and haven't found a single name for this way of parameter parsing. Of the languages that I've looked into, only Ruby and PHP support this format natively.

like image 53
rdvdijk Avatar answered Sep 25 '22 03:09

rdvdijk


It's not called anything, AFAIK, other than "sending parameters". If anything, it's called "parameter [type] conversion", where Rails just "converts" it into a hash.

Other frameworks go further, using the parameter names as expressions used to create typed objects initialized with type-converted parameter values.

All parameters are is a string value with a name. Any/all structure is imposed by the language/framework in use on the server side; what it gets transformed to is 100% dependent on that language/framework, and what that conversion consists of would determine what it would be (reasonable) called.

like image 44
Dave Newton Avatar answered Sep 26 '22 03:09

Dave Newton