假设我们有一个哈希表:
flash = {}
flash[:error] = "This is an error."
flash[:info] = "This is an information."
我想将它转换为字符串:
"<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
这解决了我的问题,但也许哈希表类中有更好的解决方案?
OrderedHash
,或者使用Ruby 1.9。 - molf