Skip to content
Browse files

PERF: change inject({}) to Hash + map

Signed-off-by: raggi <jftucker@gmail.com>
  • Loading branch information...
1 parent 12d852a commit e9d7699b0cf92b8644c42ee0497cf8e3671f6c66 @spastorino spastorino committed with raggi
Showing with 11 additions and 18 deletions.
  1. +5 −6 lib/rack/request.rb
  2. +6 −12 lib/rack/utils.rb
View
11 lib/rack/request.rb
@@ -50,9 +50,9 @@ def media_type
# { 'charset' => 'utf-8' }
def media_type_params
return {} if content_type.nil?
- content_type.split(/\s*[;,]\s*/)[1..-1].
+ Hash[content_type.split(/\s*[;,]\s*/)[1..-1].
collect { |s| s.split('=', 2) }.
- inject({}) { |hash,(k,v)| hash[k.downcase] = v ; hash }
+ map { |k,v| [k.downcase, v] }]
end
# The character set of the request body if a "charset" media type
@@ -212,10 +212,9 @@ def cookies
# precede those with less specific. Ordering with respect to other
# attributes (e.g., Domain) is unspecified.
@env["rack.request.cookie_hash"] =
- Utils.parse_query(@env["rack.request.cookie_string"], ';,').inject({}) {|h,(k,v)|
- h[k] = Array === v ? v.first : v
- h
- }
+ Hash[Utils.parse_query(@env["rack.request.cookie_string"], ';,').map {|k,v|
+ [k, Array === v ? v.first : v]
+ }]
end
end
View
18 lib/rack/utils.rb
@@ -308,14 +308,9 @@ def each
end
def to_hash
- inject({}) do |hash, (k,v)|
- if v.respond_to? :to_ary
- hash[k] = v.to_ary.join("\n")
- else
- hash[k] = v
- end
- hash
- end
+ Hash[map do |k, v|
+ [k, v.respond_to?(:to_ary) ? v.to_ary.join("\n") : v]
+ end]
end
def [](k)
@@ -423,10 +418,9 @@ def replace(other)
# Responses with HTTP status codes that should not have an entity body
STATUS_WITH_NO_ENTITY_BODY = Set.new((100..199).to_a << 204 << 304)
- SYMBOL_TO_STATUS_CODE = HTTP_STATUS_CODES.inject({}) { |hash, (code, message)|
- hash[message.downcase.gsub(/\s|-/, '_').to_sym] = code
- hash
- }
+ SYMBOL_TO_STATUS_CODE = Hash[HTTP_STATUS_CODES.map { |code, message|
+ [message.downcase.gsub(/\s|-/, '_').to_sym, code]
+ }]
def status_code(status)
if status.is_a?(Symbol)

0 comments on commit e9d7699

Please sign in to comment.
Something went wrong with that request. Please try again.