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".
in nice one liner ;)
I found something like that:
flash.to_a.collect{|item| "<div class='#{item[0]}'>#{item[1]}</div>"}.join
That solves my problem but maybe there is nicer solution built in hashtable class?
The only problem with solutions presented so far is that you usually need to list flash messages in particular order - and hash doesn't have it, so IMHO it's better use pre-defined array.
Or maby?
Hash
includesEnumerable
, so you can usecollect
:You can get the keys in the hash using
and then from there you can build a new array of strings and then join them. So something like
Does that do the trick?
inject
is infinitely handy: