Skip to content

Array to Hash snippet #3

@birula

Description

@birula

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

ary = [ [:foo, "bar"], [:fii, "baz"] ].flatten    #=> [ :foo, "bar", :fii, "baz"]
Hash[ary]                                         #=> {"foo"=> "bar", "fii"=> "baz"}

Hash[*content_type.split(/\s*[;,]\s*/)[1..-1].
  collect { |s| s.split('=', 2) }.
  map { |k,v| [k.downcase, v] }.flatten]

This may be not a big thing but I brought it on because I've used it a couple of times.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions