Konvertieren von hash-string in Ruby

Sagen wir, wir haben ein hash:

flash = {}
flash[:error] = "This is an error."
flash[:info] = "This is an information."

Möchte ich es in einen string konvertieren:

"<div class='error'>This is an error.</div><div class='info'>This is an information".

in nice one liner 😉

Fand ich, dass so etwas wie:

flash.to_a.collect{|item| "<div class='#{item[0]}'>#{item[1]}</div>"}.join

Löst mein problem, aber vielleicht gibt es schönere Lösung gebaut, in hashtable-Klasse?

Schreibe einen Kommentar