class Hash
Public Class Methods
from_array(array = [])
click to toggle source
# File lib/stella/core_ext.rb, line 532 def self.from_array(array = []) h = Hash.new array.size.times do |t| h[t] = array[t] end h end
Public Instance Methods
dump(format)
click to toggle source
# File lib/stella/core_ext.rb, line 498 def dump(format) respond_to?(:"to_#{format}") ? send(:"to_#{format}") : raise("Unknown format") end
flatten()
click to toggle source
Courtesy of Julien Genestoux
# File lib/stella/core_ext.rb, line 471 def flatten 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 end
to_http_params()
click to toggle source
Courtesy of Julien Genestoux See: stackoverflow.com/questions/798710/how-to-turn-a-ruby-hash-into-http-params NOTE: conflicts w/ HTTParty 0.7.3 when named “to_params”
# File lib/stella/core_ext.rb, line 505 def to_http_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}]", URI::Escape.escape(v)] else params << "#{parent}[#{k}]=#{URI::Escape.escape(v)}&" end end end params.chop! params end
to_json(*args)
click to toggle source
# File lib/stella/core_ext.rb, line 465 def to_json(*args) Yajl::Encoder.encode(self) end