Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat Ruby "style" issues can you find in this snippet?
    primarykey
    data
    text
    <p>I applied for a Ruby job recently and was asked to code a method to "Write a Ruby function named "query_to_hash" which takes a single String argument as an HTTP query string (e.g. <code>foo=bar&amp;abc=1%202%203</code>) and returns a Hash of the name/value pairs like following: </p> <pre><code> {"foo" =&gt; "bar", "abc" =&gt; "1 2 3"} </code></pre> <p>I provided the attached code example and got some feedback to the effect that my style of Ruby was not what they wanted.</p> <p>I'm curious now to know what style issues developers would see in the attached and would appreciate constructive feedback.</p> <pre><code>require 'rubygems' require 'uri' require 'rack' include Rack::Utils $SAFE = 3 # HTTP Query string (from wikipedia) #field1=value1&amp;field2=value2&amp;field3=value3... # The query string is composed of a series of field-value pairs. # Within each pair, the field name and value are separated by an equals sign. The equals sign may be omitted if the value is an empty string. # The series of pairs is separated by the ampersand, '&amp;' (or semicolon, ';' for URLs embedded in HTML and not generated by a &lt;form&gt;...&lt;/form&gt;; see below). # While there is no definitive standard, most web frameworks allow multiple values to be associated with a single field[3][4]: # field1=value1&amp;field1=value2&amp;field1=value3... def query_to_hash(qry, sep = '&amp;') # assume input string conforms to spec (no validation) # assume only &amp; or ; is used - not both # return a null string if value is not defined # return null hash if query is null string # return array of values in hash if field has multiple values #@qry = qry.gsub(/%20/, " ") @qry = URI.unescape(qry) rtn = Hash.new {|h,k| h[k]=[]} if @qry == "" then # return an empty hash # return {} else qry_a = @qry.split(sep) end qry_a.each do |fv_pair| pair = fv_pair.split('=') # append multiple values if needed and ensure that null values are accommodated # rtn[pair[0]] &lt;&lt; pair[1] ||= "" end # collapse array if it contains only one item # rtn.each{|k,v| rtn[k] = *v if v.length == 1} end puts "Using 'query_to_hash' method:" puts test_values = %w[foo=bar&amp;abc=1%202%203 foo&amp;abc=1%202%203 foo=&amp;abc=1%202%203 foo=bar&amp;foo=boo&amp;abc=1%202%203 ] test_values.each { |v| puts "#{sprintf("%30s",v)} is returned as #{query_to_hash(v).inspect}" } test_values = %w[ foo=bar;foo=boo;abc=1%202%203 foo=bar;foo=boo;abc=1%0A2%203 foo=bar;foo=boo;abc=1%092%0D3 ] test_values.each { |v| puts "#{sprintf("%30s",v)} is returned as #{query_to_hash(v, ';').inspect}" } puts "#{sprintf("%30s", "null string")} is returned as #{query_to_hash("").inspect}" # compare with Rack::Utils::parse_query # puts puts "Using 'Rack::Utils::parse_query' method:" puts test_values = %w[foo=bar&amp;abc=1%202%203 foo&amp;abc=1%202%203 foo=&amp;abc=1%202%203 foo=bar&amp;foo=boo&amp;abc=1%202%203 ] test_values.each { |v| puts "#{sprintf("%30s",v)} is returned as #{parse_query(v).inspect}" } test_values = %w[ foo=bar;foo=boo;abc=1%202%203 foo=bar;foo=boo;abc=1%0A2%203 foo=bar;foo=boo;abc=1%092%0D3 ] test_values.each { |v| puts "#{sprintf("%30s",v)} is returned as #{parse_query(v, ';').inspect}" } puts "#{sprintf("%30s", "null string")} is returned as #{parse_query("").inspect}" </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload