Parent

Files

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
def self.from_array(array = [])
  h = Hash.new
  array.size.times do |t|
    h[t] = array[t]
  end
  h
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
def to_params
  params = ''
  stack = []
  
  each do |k, v|
    if v.is_a?(Hash)
      stack << [k,v]
    elsif v.is_a?(Array)
      stack << [k,Hash.from_array(v)]
    else
      params << "#{k}=#{v}&"
    end
  end
  
  stack.each do |parent, hash|
    hash.each do |k, v|
      if v.is_a?(Hash)
        stack << ["#{parent}[#{k}]", v]
      else
        params << "#{parent}[#{k}]=#{v}&"
      end
    end
  end
  
  params.chop! # trailing &

  params
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.