在Ruby中哈希转换为字符串 [英] Converting hash to string in Ruby
本文介绍了在Ruby中哈希转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我们说我们有一个哈希值:
Let's say we have a hash:
flash = {}
flash[:error] = "This is an error."
flash[:info] = "This is an information."
我想将其转换为字符串:
I would like to convert it to a string:
"<div class='error'>This is an error.</div><div class='info'>This is an information".
在漂亮的单行;)
我发现了这样的事情:
flash.to_a.collect{|item| "<div class='#{item[0]}'>#{item[1]}</div>"}.join
这是解决我的问题,但也许有内置的Hashtable类更好的解决方案?
That solves my problem but maybe there is nicer solution built in hashtable class?
推荐答案
flash.collect { |k, v| "<div class='#{k}'>#{v}</div>" }.join
这篇关于在Ruby中哈希转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文