如何解析散列的字符串表示 [英] How to parse a string representation of a hash

查看:127
本文介绍了如何解析散列的字符串表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 {:account_id =>> ; 4444,:deposit_id => 3333}


解决方案

猜猜我从来没有发布我的解决方法... ...在这里,

 #将哈希降下
stringy_hash = account_id => 4444,deposit_id => 3333

#将字符串转换为哈希值
哈希值[stringy_hash.split(,)。collect {| x | x.strip.split(=>)}]


I have this string and I'm wondering how to convert it to a Hash.

"{:account_id=>4444, :deposit_id=>3333}"

解决方案

Guess I never posted my workaround for this... Here it goes,

# strip the hash down
stringy_hash = "account_id=>4444, deposit_id=>3333"

# turn string into hash
Hash[stringy_hash.split(",").collect{|x| x.strip.split("=>")}]

这篇关于如何解析散列的字符串表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆