-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Array to Hash snippet #3
Comments
what does this other line? Hash[content_type.split(/\s[;,]\s*/)[1..-1]. |
@birula I think you are missing splat operator Another interesting |
@samnang You're right i forgot * before the array, just don't now why is it used. Doesn't it sound like you're trying to map a [key, value] pairs and collect anything else? It looks silly but i use those methods that way. |
@Monomachus this line convert the header information from a request "text/html; charset=utf-8" into an array [["charset", "utf-8"]] and then to a hash { "charset" => "utf-8" } If you look closely it excludes the "text/html" from the string in the first step. |
There's another interesting thing in this code, after the first split it gets the last element with [1..-1] why not just use the .last method ? content_type.split(/\s*[;,]\s*/)[1..-1]
content_type.split(/\s*[;,]\s*/).last |
@birula, try more than one media parameters.
|
@tomykaira you're completely right using last in this case is wrong. |
would it be more efficiently? (or not?) content_type.split(/\s*[;,]\s*/).tap(&:shift) |
I was looking around the code and found this piece of code that turns an array into a Hash. The basic idea is to flatten an array to create a list of key, value and then pass it into a Hash[].
https://github.com/codereading/rack/blob/rack-1.4/lib/rack/request.rb#L57
This may be not a big thing but I brought it on because I've used it a couple of times.
The text was updated successfully, but these errors were encountered: