Parent

Hash

Public Class Methods

from_array(array = []) click to toggle source

Builds a hash from an array with keys as array indices.

    # File lib/em-http/core_ext/hash.rb, line 44
44:   def self.from_array(array = [])
45:     h = Hash.new
46:     array.size.times do |t|
47:       h[t] = array[t]
48:     end
49:     h
50:   end

Public Instance Methods

to_params() click to toggle source

Stolen partially from Merb : noobkit.com/show/ruby/gems/development/merb/hash/to_params.html Convert this hash to a query string:

  
  { :name => "Bob",
    :address => {
      :street => '111 Ruby Ave.',
      :city => 'Ruby Central',
      :phones => ['111-111-1111', '222-222-2222']
    }
  }.to_params
  #=> "name=Bob&address[city]=Ruby Central&address[phones]=111-111-1111222-222-2222&address[street]=111 Ruby Ave."
    # File lib/em-http/core_ext/hash.rb, line 14
14:   def to_params
15:     params = ''
16:     stack = []
17:     
18:     each do |k, v|
19:       if v.is_a?(Hash)
20:         stack << [k,v]
21:       elsif v.is_a?(Array)
22:         stack << [k,Hash.from_array(v)]
23:       else
24:         params << "#{k}=#{v}&"
25:       end
26:     end
27:     
28:     stack.each do |parent, hash|
29:       hash.each do |k, v|
30:         if v.is_a?(Hash)
31:           stack << ["#{parent}[#{k}]", v]
32:         else
33:           params << "#{parent}[#{k}]=#{v}&"
34:         end
35:       end
36:     end
37:     
38:     params.chop! # trailing &
39:     params
40:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.