相关疑难解决方法(0)

在Ruby中将哈希转换为字符串

假设我们有一个哈希:

flash = {}
flash[:error] = "This is an error."
flash[:info] = "This is an information."
Run Code Online (Sandbox Code Playgroud)

我想将其转换为字符串:

"<div class='error'>This is an error.</div><div class='info'>This is an information".
Run Code Online (Sandbox Code Playgroud)

在漂亮的一个班轮;)

我发现了类似的东西:

flash.to_a.collect{|item| "<div class='#{item[0]}'>#{item[1]}</div>"}.join
Run Code Online (Sandbox Code Playgroud)

这解决了我的问题,但也许在哈希表类中有更好的解决方案?

ruby arrays hash

8
推荐指数
1
解决办法
1万
查看次数

标签 统计

arrays ×1

hash ×1

ruby ×1