String/Symbol Lookup for Params Hash
Reported by Matt Todd | December 28th, 2008 @ 07:58 AM
Adding ticket for conversation going on at:
http://groups.google.com/group/r...
The conversation covers numerous topics so this ticket reflects specifically the proposal for lookup keys supporting both string and symbol.
The idea was mentioned that we may want to consider allowing both String and Symbol lookup for the params. For example:
#=> both refer to the foo=bar portion of the query string:
request.GET[:foo]
request.GET["foo"]
The benefit is simplicity and convenience though performance
(certainly) takes a hit.Comments and changes to this ticket
-
Dan Kubb (dkubb) December 28th, 2008 @ 08:36 AM
One small implementation note would be to store the parameter name as a String underneath. Otherwise it would be simple to construct a request that eats up memory due to the fact that Symbols are not GC'd.
See Extlib's Mash implementation for an idea how this should be done:
-
Ryan Tomayko December 28th, 2008 @ 08:37 AM
I've used the following idiom for this a couple of times:
params = Hash.new { |hash,key| hash[key.to_s] if Symbol === key }
It's definitely not the fastest thing but it works well and is fairly simple. It does assume that keys will always be set as Strings, however.
-
Matt Todd December 28th, 2008 @ 08:47 AM
I don't think that supporting symbol lookup is necessary... it's convenient, and I'd not oppose it, but I think to cleanly cover it (without limiting functionality), you'd want to use something like Mash or HashWithIndifferentAccess which I think isn't necessary and perhaps a bit extraneous to what I think Rack really stands for (a best-practices of web framework development in Ruby).
Just my $0.02.
-
Ryan Tomayko December 29th, 2008 @ 10:41 AM
Yeah. I don't think this belongs in core either.
-1
It might not be a bad idea to have some kind of utility for achieving this in rack-contrib, though.
-
chneukirchen (at gmail) December 29th, 2008 @ 08:06 PM
- State changed from new to invalid
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.