Hash转换为查询字符串

2015-02-24 0 718
Hash转换为查询字符串
#依赖addressable/uri
require 'addressable/uri'

def _to_query_string(hash)
    if hash.instance_of? String
      URI.encode hash
    else
      uri = Addressable::URI.new
      uri.query_values = hash
      uri.query
    end
end

#使用方法:
query_string = _to_query_string( {'name'=>'hello', 'hello'=>'world'} )
puts query_string 
#输出:name=hello&pwd=world
query_string = _to_query_string "name=hello&pwd=world"
puts query_string
#输出:name=hello&pwd=world

遇见资源网 ruby Hash转换为查询字符串 http://www.ox520.com/16280.html

上一篇: 缺失的define_class
下一篇: Defficient Number
常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务